my issue is very related to the following:
Why am I getting a Hibernate LazyInitializationException in this Spring MVC web application when the data displays correctly?
I have the following properties on a particular entity:
@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;
As you might have noticed I have an Eager Fetch type on both relations, but it is giving the following:
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
I must access both lists List<ColumnFormat> columnList
and List<Originator> originators
on different beans, but if both are of Fetch Type Eager I get the above problem, and if one of them is Lazy I get the following:
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
Is there a way to retrieve the lists on the different beans without having these problems?