aspose file tools*
The moose likes Object Relational Mapping and the fly likes MultipleBagFetchException: cannot simultaneously fetch multiple bags is there a workaround Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "MultipleBagFetchException: cannot simultaneously fetch multiple bags is there a workaround" Watch "MultipleBagFetchException: cannot simultaneously fetch multiple bags is there a workaround" New topic
Author

MultipleBagFetchException: cannot simultaneously fetch multiple bags is there a workaround

Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 582
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

Joined: Jun 29, 2002
Posts: 582
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

Joined: Mar 07, 2010
Posts: 1680
    
    7

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

[How To Ask Questions][Read before you PM me]
Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 582
Thanks Bill will try that, just to see if the data in the lists is returned correct no duplicates or other poblems.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MultipleBagFetchException: cannot simultaneously fetch multiple bags is there a workaround