本文介绍了当字段渴望时出现LazyInitializationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的课程 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是另一个实体.
codesc is another entity.
在我的代码中,我写道:
In my code I wrote:
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)
如果获取类型渴望,如何获得Lazy异常?
how can I get Lazy exception if the fetch type is eager?
推荐答案
load()
不会立即加载实体,它返回一个惰性代理,该代理在第一个方法调用时获取实际数据.在大多数情况下,您需要使用 get()
而不是 load()
.
load()
doesn't load an entity immediately, it returns a lazy proxy that fetches the real data at the first method call. In most cases you need to use get()
instead of load()
.
这篇关于当字段渴望时出现LazyInitializationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!