@ManyToOne configuration lazy loading, if the associated primary key column,
@ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "billid", insertable = false,updatable = false, nullable = false) private Bill bill ;
@Setter @Getter @Entity @DynamicUpdate @Table(name = "Bill") public class Bill implements Serializable { /** * 主键 */ @Id @Column(name = "ID") private String id; @Column(name = "SID") private String sid; }
Lazy loading is normal.
But if the associated Address sid is not the id, then it is a problem loading delay. The following code:
@ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "billid", referencedColumnName = "sid", insertable = false,updatable = false, nullable = false) private Bill bill;
At this time of the solution, we need to understand the mechanisms of PersistentAttributeInterceptable.
@DynamicUpdate @Entity @Setter @Getter @Table ( name ="BillDETAIL" ) public class BillDetail implements PersistentAttributeInterceptable, Serializable {
@ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "billid", referencedColumnName = "sid", insertable = false,updatable = false, nullable = false) @LazyToOne(LazyToOneOption.NO_PROXY) private Bill bill; public Bill getBill() { if (interceptor!=null) { return (Bill)interceptor.readObject(this, "bill", bill); } return bill; } public void setBill(BillView bill) { if (interceptor!=null) { this.billView = (Bill) interceptor.writeObject(this,"owner", this.bill,bill); return ; } this.bill = bill; } @Transient private PersistentAttributeInterceptor interceptor; @Override public PersistentAttributeInterceptor $$_hibernate_getInterceptor() { return interceptor; } @Override public void $$_hibernate_setInterceptor(PersistentAttributeInterceptor interceptor) { this.interceptor = interceptor; } }
reference:
https://blog.csdn.net/johnf_nash/article/details/86557107
https://blog.csdn.net/windsigirl123/article/details/60957632