使用JPA(EntityManager)和Hibernate。FetchType.LAZY沒有從db獲取對象
我有一個類有3個集合,其中一個具有FetchType.EAGER,另外兩個具有LAZY。如果我把它們中的3個放在EAGER中,我會得到一個例外,因爲它只能有一個。在這條路上,當我嘗試使用延遲列表中的一個,我得到一個異常:
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;
}
編輯: 異常,當我有一個實體不止一個心急的部署(或我的理解日誌)
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
問候。
沒有理由不允許多個渴望的元素集合,你確定你得到的異常是關於那個嗎? – remigio
只要附加集合(超出第一個集合)定義了@OrderColumn,您就應該能夠定義並熱切地獲取多個集合。 – Perception
與@orderClumn我能夠得到的callections! FetchType.EAGER完美運作。 Regards Perception –