Organizational Research By

Surprising Reserch Topic

multiple fetches with eager type in hibernate with jpa

multiple fetches with eager type in hibernate with jpa  using -'java,hibernate,tomcat,jpa'

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


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?

asked Oct 7, 2015 by virendra.bajaj
0 votes

Related Hot Questions

2 Answers

0 votes
answered Oct 7, 2015 by ankitarajoria4
0 votes
answered Oct 7, 2015 by ashish singh