使用 JPA (EntityManager) 和休眠。
我有一个有 3 个集合的类,一个有 FetchType.EAGER,另外两个有 LAZY。如果我将其中 3 个放在 EAGER 中,我会得到一个例外,因为它只能有一个。这样,当我尝试使用 LAZY 列表之一时,我得到一个异常:
no session or session was closed: javax.faces.el.EvaluationException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role:
那么,当我要求它们时,我怎样才能得到 2 个集合?
我的课:
public class Event implements....
....
....
@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name="friendsList")
private List<Long> friends;
@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name="carsList")
private List<Long> cars;
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name="housesList")
private List<Long> houses;
我试过了,但还是不行
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public IEvent synchronizeCarsList(IEvent companyToSynchronize){
if(companyToSynchronize == null)
throw new IllegalArgumentException();
if(entityManager != null) {
List<Long> cars = companyToSynchronize.getSelectedCarsIdList();
}
return companyToSynchronize;
}
编辑:当我在一个实体中有多个 EAGER(或我从日志中了解的内容)时部署异常
Caused by: org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags
at org.hibernate.loader.BasicLoader.postInstantiate(BasicLoader.java:94) [:3.6.6.Final]
at org.hibernate.loader.entity.EntityLoader.<init>(EntityLoader.java:119) [:3.6.6.Final]
at org.hibernate.loader.entity.EntityLoader.<init>(EntityLoader.java:71) [:3.6.6.Final]
at org.hibernate.loader.entity.EntityLoader.<init>(EntityLoader.java:54) [:3.6.6.Final]
at org.hibernate.loader.entity.BatchingEntityLoader.createBatchingEntityLoader(BatchingEntityLoader.java:133) [:3.6.6.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.createEntityLoader(AbstractEntityPersister.java:1914) [:3.6.6.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.createEntityLoader(AbstractEntityPersister.java:1937) [:3.6.6.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.createLoaders(AbstractEntityPersister.java:3205) [:3.6.6.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.postInstantiate(AbstractEntityPersister.java:3191) [:3.6.6.Final]
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:348) [:3.6.6.Final]
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872) [:3.6.6.Final]
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:906) [:3.6.6.Final]
... 105 more
问候。