2014-02-21 21 views
0

時,我有兩個實體之間的一個一對多的關係,相關部分是這樣的:的PersistenceException設置FetchType

「一個部分」(車):

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

的「多部分」(項目):

@ManyToOne 
private Cart cart; 

這個工作,只要我避免傳遞一個FetchType@OneToMany。當添加FetchType(如上圖所示),我得到一個的PersistenceException(不是非常有幫助):

[PersistenceUnit:defaultPersistenceUnit]無法建立 的EntityManagerFactory

還有一些其他實體我具有一對多關係的代碼看起來與上面的完全相同,並且在傳遞FetchType時不會導致此異常。這是爲什麼?我正在使用Hibernate。

編輯:

在情況下,重要的是:車實體是很多部分組成的,而該項目的實體是上面沒有顯示其他關係的一個部分。

EDIT2:

堆棧跟蹤:

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] 
+0

註釋它雖然例外似乎真的什麼也不說,它總是幫助我們,當你在這裏發表的堆棧跟蹤 – Leo

+0

真,添加堆棧跟蹤 – alapeno

+0

謝謝。你看,現在我們有了新的東西。這個鏈接有用嗎? http://stackoverflow.com/questions/4334970/hibernate-cannot-simultaneously-fetch-multiple-bags – Leo

回答

0

對於我來說,似乎你有另外一個集合被即時加載在你的實體。考慮將其中的一個更改爲fetch=FetchType.LAZY

如果沒有可能,從你的@OneToMany關係刪除fetch=FetchType.EAGER@LazyCollection(LazyCollectionOption.FALSE)

相關問題