我有这样的课程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异常?
答案 0 :(得分:4)
load()
不会立即加载实体,它会返回一个惰性代理,该代理在第一次方法调用时获取实际数据。在大多数情况下,您需要使用get()
代替load()
。
答案 1 :(得分:0)
找到此link。如果Codesc实体中有CollectionOfElements
注释,则会引发关于延迟初始化的异常。