gpt4 book ai didi

java - 字段急切时出现 LazyInitializationException

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

我有这样的 OmQcActivity 类:

@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Table(name="OM_QC_ACTIVITY")
public class OmQcActivity{
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="STATUS_ID")
private Codesc status;
}

codesc 是另一个实体。

在我的代码中我写道:

OmQcActivity  myactivity = findQCActivityById(5);
Codesc status = myactivity.getCodesc();

@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public OmQcActivity findQCActivityById(Long id) {
return session.load(persistentClass, id);
}

但是,我得到:

org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:86)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:140)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
at com.mycompany.model.OmQcActivity_$$_javassist_11.getStatus(OmQcActivity_$$_javassist_11.java)

如果获取类型是急切的,我如何获得惰性异常?

最佳答案

load() 不会立即加载实体,它会返回一个在第一次方法调用时获取真实数据的惰性代理。在大多数情况下,您需要使用 get() 而不是 load()

关于java - 字段急切时出现 LazyInitializationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5503771/

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