Fetching Nested collections in JPA
While coding in JPA with hibernate underneath I got an exception org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags. I was using “LIST” for the association and fetching eagerly
@ManyToMany(cascade={CascadeType.MERGE,CascadeType.REFRESH},fetch = FetchType.EAGER) @JoinTable(name="class_subject", joinColumns={@JoinColumn(name="classid")}, inverseJoinColumns=@JoinColumn(name="subjectid")) private List<SubjectDetails> subjects;
The problem seems to be two fold:
- JPA allows only two level of nesting for collection, Parententity->Childentity is fine but it does not allow Parententity->childentity->SubChildEntity
- Using List for collection instead of SET -remember a SET guarantees unique elements while list does not
What could be solution:
- Use SET instead of List and thats what I have done for the moment
private SET<SubjectDetails> subjects;
Posted on December 22, 2012, in Hibernate, Java and tagged JPA, MultipleBagFetchException, NestedCollections. Bookmark the permalink. Leave a comment.
Leave a comment
Comments 0