gpt4 book ai didi

java - 延迟初始化集合失败,从 org.hibernate.classic.Lifecycle 的 onload 方法访问时没有 session 或 session 被关闭

转载 作者:行者123 更新时间:2023-12-01 23:59:24 25 4
gpt4 key购买 nike

代码

l_details

pin 具有引用 x_details 主键 的外部约束,这里我使用 XDetails 类中的单向关系。

实体:

    @Entity
@Table(name = "x_details")
public class XDetails implements Lifecycle {
private int y;
....
.....
private Set<LDetails> lDetails = new HashSet<LDetails>(0);
.........
..........
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name="pin")
public Set<LDetails> getLDetails() {
return this.lDetails;
}

@Override
public void onLoad(Session s, Serializable id) {
System.out.println("inside load");
try{
if((lDetails!=null)){
System.out.println("size of lDetails"+lDetails.size());
}
}
catch(Exception exception){
exception.printStackTrace();
}
}
}

@Entity
@Table(name = "l_details")
public class LDetails {

private Integer id1;
private Integer pin;
....
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id1", unique = true, nullable = false)
public Integer getId1() {
return this.id1;
}

public void setId1(Integer id1) {
this.id1 = id1;
}
@Column(name="pin")
public Integer getPin() {
return this.pin;
}

public void setPin(Integer pin) {
this.pin = pin;
}

}
主线程:
    Session session=HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Criteria criteria=session.createCriteria(XDetails.class);
criteria.createAlias("lDetails", "status",CriteriaSpecification.INNER_JOIN);
criteria.setFetchMode("status", FetchMode.JOIN);
List<XDetails> details=criteria.list();
session.getTransaction().commit();
异常(在 onload 内):
    org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed
at rg.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:122)
at org.hibernate.collection.PersistentSet.size(PersistentSet.java:162)
at com.hibernate.test.pendingdict3.XDetails.onLoad(XDetails.java:588)
at org.hibernate.event.def.DefaultPostLoadEventListener.onPostLoad(DefaultPostLoadEventListener.java:73)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:234)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:898)
at org.hibernate.loader.Loader.doQuery(Loader.java:773)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
at org.hibernate.loader.Loader.doList(Loader.java:2449)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2192)
at org.hibernate.loader.Loader.list(Loader.java:2187)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:119)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1706)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
at com.hibernate.test.main4.HibernateStandaloneMain.main(HibernateStandaloneMain.java:24)
我正在使用 Hibernate 3.5.6 Final 依赖项、mysql 数据库,并且正在运行 hibernatestandalone 应用程序。
# onload method
@Override
public void onLoad(Session s, Serializable id) {
System.out.println("inside load");
try{
if((lDetails!=null)){
System.out.println("size of lDetails"+lDetails.size());
}
}
catch(Exception exception){
exception.printStackTrace();
}
}

最佳答案

问题是你选择了LAZY正在加载,但当您第一次访问集合的元素时,您的 session 已经关闭。您需要EAGER获取您的Set<LDetails>或保持 session 打开,直到所有操作完成。

关于java - 延迟初始化集合失败,从 org.hibernate.classic.Lifecycle 的 onload 方法访问时没有 session 或 session 被关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15114049/

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