Reputation: 1313
I have an entity which contains:
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "assessment")
@OrderBy(value = "order ASC")
private List<AssessmentPart> assessmentParts = new LinkedList<>();
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "assessment")
private List<AssessmentText> texts = new LinkedList<>();
as you see there are two collections which needs to be eagerly loaded. This is not working and hibernate throws an exception:
Caused by: org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags
That's because Hibernate can't fetch multiple collections with one go. But if I change the List
to Set
and the LinkedList
to HashSet
this part works fine but the other - more annoying issue occurs.
When I am trying to get the entity from the database using:
entityManager.find(entityClass, primaryKey);
It fails with:
org.hibernate.AssertionFailure: null identifier
I am sure that ID I am passing to find
method is not null, I've debugged and I am sure of this. It somehow disappears inside Hibernate.
If I change collection types to LAZY
everything just works without errors but there are some circumstances where I need to use EAGER
.
Does anybody have a solution how to fix it? Either I could have a set but prevent assertion error from occurring or I could have a list but somehow avoid multiple fetch bags error.
I am using:
Hibernate 4.2.2.Final
Tomcat 7
JPA 2.0
JDK 1.7
EDIT
I've just discovered that adding @Fetch(FetchMode.SELECT)
fixes the issue and I can use multiple list with EAGER
type, but is there anyway to solve this by not using Hibernate specific annotations? And why it fixed the issue in the first place?
Upvotes: 39
Views: 64919
Reputation: 7817
The root cause of the problem is that when Hibernate fetches SQL query results there is no simple way to tell which child element belongs to which collection. See this blog entry for more detailed explanation with an example. To summarize you have following workarounds:
@Fetch(FetchMode.SELECT)
@IndexColumn(name="LIST_INDEX")
Upvotes: 67
Reputation: 3132
If you are using Hibernate and you do not care using Hibernate annotations :
Annotate your collection fields with:
@LazyCollection(LazyCollectionOption.FALSE)
Remember to remove the fetchType attribute from the @OneToMany annotation.
Upvotes: 14