@ManyToOne配置延迟加载,如果是关联主键列,则在从表里这样配置:(主表CheckRec ,从表CheckDiag )
```
@ManyToOne
@JoinColumn(name = "CHECK_REC_ID")
private CheckRec checkRec;
```
此注解表示 主表CheckRec 的主键(id)与 从表的 CHECK_REC_ID字段相关联;延迟加载是正常的。
但如果关联的CheckRec 的不是主键的话,就延时加载就有问题。如下代码:
```language
@ManyToOne
@JoinColumn(name="BUSINESS_NO", referencedColumnName="BUSINESS_NO",insertable = false, updatable = false)
private CheckRec checkRec;
```
这时的解决方案,需要了解PersistentAttributeInterceptable的机制。
```language
public class CheckDiag implements PersistentAttributeInterceptable,Serializable{
private PersistentAttributeInterceptor interceptor;
@Override
public PersistentAttributeInterceptor $$_hibernate_getInterceptor() {
// TODO Auto-generated method stub
return null;
}
@Override
public void $$_hibernate_setInterceptor(PersistentAttributeInterceptor interceptor) {
// TODO Auto-generated method stub
}
private com.dhcc.cdbp.entity.emr.CheckRec checkRec;
@ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
@JoinColumn(name="BUSINESS_NO", referencedColumnName="BUSINESS_NO",insertable = false, updatable = false)
@LazyToOne(LazyToOneOption.NO_PROXY)
public com.dhcc.cdbp.entity.emr.CheckRec getCheckRec() {
if (interceptor != null) {
return (CheckRec) interceptor.readObject(this, "checkRec", checkRec);
}
return checkRec;
}
public void setCheckRec(com.dhcc.cdbp.entity.emr.CheckRec checkRec) {
if (interceptor != null) {
this.checkRec = (CheckRec) interceptor.writeObject(this, "owner", this.checkRec, checkRec);
return;
}
this.checkRec = checkRec;
}
}
```
1.实现PersistentAttributeInterceptable接口
2.将@ManyToOne等注解放到getter方法上并加入如上代码
3.将字段的注解加到getter方法上(从表);因为注解要么全部放属性上或者全部放getter上,保持一致