0

I have a one-to-many relationship between two entities, the relevant parts look like this:

The "one-part" (Cart):

@OneToMany(mappedBy="cart", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Item> items;

The "many-part" (Item):

@ManyToOne
private Cart cart;

This works as long as I avoid to pass a FetchType to @OneToMany. When adding a FetchType (as shown above) I get a PersistenceException (not very helpful):

[PersistenceUnit: defaultPersistenceUnit] Unable to build EntityManagerFactory

There are some other entities in my code having one-to-many-relationships that look exactly like the one above and do not procuce this exception when passing the FetchType. Why is that? I'm using Hibernate.

edit:

In case it is important: the Cart-Entity is the many-part whereas the Item-Entity is the one-part of other relationships not shown above.

edit2:

Stacktrace:

PersistenceException: [PersistenceUnit: defaultPersistenceUnit] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915) ~[hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:57) ~[hibernate-entitymanager-.6.9.Final.jar:3.6.9.Final]
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63) ~[hibernate-jpa-2.0-api.jar:1.0.1.Final]
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) ~[hibernate-jpa-2.0-api.jar:1.0.1.Final]
at play.db.jpa.JPAPlugin.onStart(JPAPlugin.java:35) ~[play-java-jpa_2.10.jar:2.2.1]
at play.api.Play$$anonfun$start$1$$anonfun$apply$mcV$sp$1.apply(Play.scala:88) ~[play_2.10.jar:2.2.1] Caused by: org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags
at org.hibernate.loader.BasicLoader.postInstantiate(BasicLoader.java:94) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.loader.entity.EntityLoader.<init>(EntityLoader.java:119) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.loader.entity.EntityLoader.<init>(EntityLoader.java:71) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.loader.entity.EntityLoader.<init>(EntityLoader.java:54) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.loader.entity.BatchingEntityLoader.createBatchingEntityLoader(BatchingEntityLoader.java:133) ~[hibernate-core-.6.9.Final.jar:3.6.9.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.createEntityLoader(AbstractEntityPersister.java:1914) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
alapeno
  • 2,724
  • 6
  • 36
  • 53
  • although the exception really seems to say nothing, it always help us when you post the stacktrace here – Leo Feb 21 '14 at 23:10
  • True, added the stacktrace – alapeno Feb 21 '14 at 23:22
  • thanks. You see, now we have something new here. Is this link useful? http://stackoverflow.com/questions/4334970/hibernate-cannot-simultaneously-fetch-multiple-bags – Leo Feb 21 '14 at 23:25

1 Answers1

0

For me it seems you have another collection being eagerly loaded in your entity. Consider changing one of them to fetch=FetchType.LAZY.

If not possible, remove the fetch=FetchType.EAGER from your @OneToMany relation and annotate it with @LazyCollection(LazyCollectionOption.FALSE)

renke
  • 1,180
  • 2
  • 12
  • 27