gpt4 book ai didi

java - hibernate LazyInitializing 悖论

转载 作者:行者123 更新时间:2023-11-30 11:33:08 30 4
gpt4 key购买 nike

我有 3 个名为DisciplineDisciplineMembershipDisciplineManagement 的实体:

一个学科有多个学科成员
一个学科有多个学科管理
DisciplineManagementDisciplineMembership

具有一对一关系

Discipline 相关 hbm 文件的相关部分如下所示:

纪律.hbm:

<class name="Discipline" table="DISCIPLINES">
<id name="id" type="java.lang.Long">
<generator class="increment" />
</id>

<set name="memberships" inverse="false" cascade="all-delete-orphan" >
<key column="disciplineId" not-null="true" />
<one-to-many class="DisciplineMembership" />
</set>

<set name="managements" inverse="false" cascade="all-delete-orphan">
<key column="disciplineId" not-null="true" />
<one-to-many class="DisciplineManagement" />
</set>

</class>

在名为 A 的函数中,我想遍历学科的 DisciplineMemberships 和 DiscipilineManagements:

public void A(){
Discipline discipline = this.getObject();
for(DisciplineMembership mem: discipline.getMembers()){
System.out.print(mem);
}
for(DisciplineManagement man: discipline.getManagements()){
System.out.print(man);
}
}

第一个 for 语句运行正确,但在第二个语句中,我捕获了 LazyInitializationException,如下所示:

13-04-27 13:54:26,115 [hibernate.LazyInitializationException (<init>:42)] ERROR: failed to lazily initialize a collection of role: Discipline.managements, no session or session was closed 
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: Discipline.managements, 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.initialize(AbstractPersistentCollection.java:368)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:186)
at ObjectManagementPart_Edit.A(ObjectManagementPart_Edit.java:6)

我很困惑这怎么可能? Discipline Session 是打开的,我可以遍历它的成员,但是当我想遍历它的管理时,hibernate 不能延迟初始化它们。

我该怎么办?

更新:我尝试使用 Hibernate.initialize() 函数,所以函数 A 现在是这样的:

public void A(){
Discipline discipline = this.getObject();
Hibernate.initialize(discipline.getMembeships());
for(DisciplineMembership mem: discipline.getMemberships()){
System.out.print(mem);
}
Hibernate.initialize(discipline.getManagements());
for(DisciplineManagement man: discipline.getManagements()){
System.out.print(man);
}
}

Membesships第一次使用Hibernate.initialize()没问题,但是在 Managements 的第二次使用中我得到了这个异常:

org.hibernate.HibernateException: collection is not associated with any session
at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:474)
at org.hibernate.Hibernate.initialize(Hibernate.java:431)
at discipline.ObjectManagementPart_Edit.A(ObjectManagementPart_Edit.java:6)

现在我更困惑了:/

最佳答案

仅仅因为您可以迭代成员资格并不能告诉您它实际上只是懒惰地获取它们。如果之前加载的成员资格( session 打开的地方),它们将保持不变。如果列表已经初始化,Hibernate.initialize(...) 将不会执行任何操作。

要获得更准确的答案,您需要描述您的 session 管理策略。 hibernate 的大部分问题根源在于 session 管理不好。

关于java - hibernate LazyInitializing 悖论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16250861/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com