如何获取@OneToMany和@ManyToMany实体

时间:2011-03-31 22:23:04

标签: java hibernate fetch

我的问题与以下内容非常相关:
Why am I getting a Hibernate LazyInitializationException in this Spring MVC web application when the data displays correctly?

我在特定实体上有以下属性:

@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  

我必须访问不同bean上的列表List<ColumnFormat> columnListList<Originator> originators,但如果两者都是获取类型 渴望,我会遇到上述问题,如果其中一个是 Lazy ,我会得到以下内容:

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上的列表?

2 个答案:

答案 0 :(得分:4)

之前没有遇到过这个问题,但只是谷歌搜索“无法同时获取多个包”在Hibernate论坛上返回this link

该链接中的blog posts之一可能包含您正在寻找的解决方案。

答案 1 :(得分: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 
     ...........
     ...........
     ...........

   }