如何获取@OneToMany和@ManyToMany实体

发布于 2024-10-29 01:10:49 字数 3403 浏览 0 评论 0原文

我的问题与以下内容密切相关:
为什么我会进入休眠状态当数据正确显示时,此 Spring MVC Web 应用程序中存在 LazyInitializationException?

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

@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; columnListList发起者在不同的bean上,但如果两者都是获取类型急切我会遇到上述问题,并且如果其中一个是懒惰 > 我得到以下信息:

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆头 2024-11-05 01:10:49

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

该链接中的一篇博客文章可能包含您的解决方案正在寻找。

Haven't encountered this problem before, but just googling "cannot simultaneously fetch multiple bags" returns this link on the Hibernate forums.

One of the blog posts in that link may contain the solution you're looking for.

吃不饱 2024-11-05 01:10:49

我删除了所有 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 
     ...........
     ...........
     ...........

   }

I deleted all fetch=FetchType.EAGER and it work correct. I´m usign Hibernate 3.5.6-Final, Spring 3.0.4 and Junit 4. I think fetch-EAGER is by default, because my object loads all children.

This is 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>

This is class:

@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 
     ...........
     ...........
     ...........

   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文