Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MultipleBagFetchException: cannot simultaneously fetch multiple bags is there a workaround

 
Tony Evans
Ranch Hand
Posts: 598
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This exception Caused by: org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags is caused by an eager fetch within a fetch

I have an entity which contains the following one to many mapping



TraderPortfoilio contains a one to many mapping


A eager fetch within an eager fetch will cause the exception cannot simultaneously fetch multiple bags

But there has to be a workaround to allow multiple eager fetches to load a parent an all its child objects into the cache at one load.
 
Tony Evans
Ranch Hand
Posts: 598
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Changing list to set worked, the difference between sets an lists is that sets dont allow duplicates, so i guess an eager fetch within an eager fetch can depending on how JPA or hibernate work under the covers can cause problems such as retieving duplicates on the select. So forcing you to use the set collection.
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That works. Also in cases where I needed the list I have used a hibernate specific annotation on the collection (see below)


In order for this to work you will need to remove the (fetch = FetchType.EAGER).

I am not sure if they addressed this yet in hibernate 4 or not but the above works for lists in 3.x
 
Tony Evans
Ranch Hand
Posts: 598
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bill will try that, just to see if the data in the lists is returned correct no duplicates or other poblems.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic