2011-03-31 84 views
5

我的問題是非常相關的以下內容:
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> columnListList<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上的列表?

+0

你可以張貼一些您所使用的檢索數據的代碼? – Nathanphan 2011-04-01 01:33:18

+0

答案可以在這裏找到:[休眠不能同時獲取多個袋子](http://stackoverflow.com/a/4335514/251745) – chepseskaf 2012-09-12 09:37:48

回答

4

以前沒有遇到過這個問題,但只是在Hibernate論壇上用google搜索「不能同時取多個包」返回this link

該鏈接中的blog posts之一可能包含您正在查找的解決方案。

+0

你給的鏈接肯定有幫助。謝謝,仍然在解決方案 – camiloqp 2011-04-01 13:25:06

+0

我會接受你的答案,因爲它解決了我提出的實際問題,但是考慮到新的要求,我必須以不同的方式解決它。 – camiloqp 2011-04-01 15:49:48

1

我刪除了所有fetch=FetchType.EAGER,它工作正確。我正在使用Hibernate 3.5.6-Final,Spring 3.0.4和Junit 4.我認爲fetch-EAGER是默認的,因爲我的對象加載了所有的孩子。

這是SessionFactory的:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" 
    p:dataSource-ref="dataSource" 
    p:packagesToScan="org.pfc.modelo"> 

    <property name="hibernateProperties"> 
     <props> 
      <prop key="dialect">org.hibernate.dialect.Oracle10gDialect</prop> 
      <prop key="show_sql">true</prop> 
      <prop key="format_sql">true</prop> 
     </props> 
    </property> 
</bean> 

這是類:

@Entity 
@Table(name="D_ACTIVIDAD") 
public class Actividad implements Serializable { 


private static final long serialVersionUID = 1L; 

@Id 
@SequenceGenerator(sequenceName="SQ_ACTIVIDAD",name="seqActividad") 
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="seqActividad") 
private Integer idActividad; 

@Column(name="FECHA",nullable=false) 
@Temporal(TemporalType.TIMESTAMP) 
private Calendar fecha; 

@Column(name="TITULO",nullable=false) 
private String titulo; 

@Column(name="TEXTO",nullable=false) 
private String texto; 

@ManyToOne 
@JoinColumn(name="IDASOCIACION") 
private Asociacion asociacion; 

@OneToMany(mappedBy="actividad",cascade=CascadeType.ALL,orphanRemoval=true) 
private List<ComentarioActividad> comentarios; 

@OneToMany(mappedBy="actividad",cascade=CascadeType.ALL,orphanRemoval=true) 
@OrderBy(value="fecha") 
private List<Documento> documentos; 

    //set and get methods 
    ........... 
    ........... 
    ........... 

    }