How to Fetch @OneToMany and @ManyToMany Entities(如何获取 @OneToMany 和 @ManyToMany 实体)
问题描述
我的问题与以下内容非常相关:
为什么我要休眠当数据正确显示时,此 Spring MVC Web 应用程序中出现 LazyInitializationException?
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;
您可能已经注意到,我在这两个关系上都有一个 Eager Fetch 类型,但它给出了以下内容:
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
我必须访问两个列表 List<ColumnFormat>columnList
和 List
在不同的 bean 上,但如果两者都是 Fetch Type Eager 我会遇到上述问题,如果其中一个是 Lazy 我得到以下信息:
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
有没有办法在没有这些问题的情况下检索不同 bean 上的列表?
Is there a way to retrieve the lists on the different beans without having these problems?
推荐答案
之前没遇到过这个问题,只是google一下不能同时获取多个包"返回这个链接在 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.
这篇关于如何获取 @OneToMany 和 @ManyToMany 实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何获取 @OneToMany 和 @ManyToMany 实体
基础教程推荐
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 降序排序:Java Map 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01