我有这样的课程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)
如果渴望获取类型,如何获得Lazy异常?
最佳答案
load()
不会立即加载实体,它会返回一个懒惰的代理,该代理在第一个方法调用时获取实际数据。在大多数情况下,您需要使用get()
而不是load()
。