我的問題是非常相關的以下內容:
Why am I getting a Hibernate LazyInitializationException in this Spring MVC web application when the data displays correctly?如何獲取@OneToMany和@ManyToMany實體
我有一個特定的實體以下屬性:
@OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.REMOVE})
@JoinColumn(referencedColumnName = "id", name = "ID_FORMAT_FILE")
private List<ColumnFormat> columnList;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "FILEFORMAT_ORIGINATOR", joinColumns = @JoinColumn(name = "FILEFORMAT_ID", referencedColumnName = "id")
, inverseJoinColumns = @JoinColumn(name = "ORIGINATOR_ID", referencedColumnName = "id"))
private List<Originator> originators;
正如你可能已經注意到我對這兩種關係都有Eager Fetch類型,但它給出以下內容:
Caused by: org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags
at org.hibernate.loader.BasicLoader.postInstantiate(BasicLoader.java:94)
at org.hibernate.loader.entity.EntityLoader.<init>(EntityLoader.java:119)
at org.hibernate.loader.entity.EntityLoader.<init>(EntityLoader.java:71)
at org.hibernate.loader.entity.EntityLoader.<init>(EntityLoader.java:54)
at org.hibernate.loader.entity.BatchingEntityLoader.createBatchingEntityLoader(BatchingEntityLoader.java:133)
at org.hibernate.persister.entity.AbstractEntityPersister.createEntityLoader(AbstractEntityPersister.java:1914)
at org.hibernate.persister.entity.AbstractEntityPersister.createEntityLoader(AbstractEntityPersister.java:1937)
at org.hibernate.persister.entity.AbstractEntityPersister.createLoaders(AbstractEntityPersister.java:3205)
at org.hibernate.persister.entity.AbstractEntityPersister.postInstantiate(AbstractEntityPersister.java:3191)
at org.hibernate.persister.entity.SingleTableEntityPersister.postInstantiate(SingleTableEntityPersister.java:728)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:348)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:902)
... 33 more
我必須訪問這兩個列表List<ColumnFormat> columnList
和List<Originator> originators
不同的豆子,但如果兩者的獲取類型渴望我得到上面的問題,如果他們中的一個是懶惰我得到如下:
Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: xxx.xxx.xxx.xxx.FileFormat.originators, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
at org.hibernate.collection.AbstractPersistentCollection.readElementExistence(AbstractPersistentCollection.java:157)
at org.hibernate.collection.PersistentBag.contains(PersistentBag.java:262)
at java.util.ArrayList.batchRemove(ArrayList.java:632)
at java.util.ArrayList.removeAll(ArrayList.java:605)
at xxx.xxx.xxx.xxx.bean.FileFormatEdit.init(FileFormatEdit.java:1040)
... 75 more
是否有辦法在不存在這些問題的情況下檢索不同bean上的列表?
你可以張貼一些您所使用的檢索數據的代碼? – Nathanphan 2011-04-01 01:33:18
答案可以在這裏找到:[休眠不能同時獲取多個袋子](http://stackoverflow.com/a/4335514/251745) – chepseskaf 2012-09-12 09:37:48