最近項目中遇到了個hibernate lazy延遲加載的問題,實體關系如下A,B
public class A {
@OneToOne(fetch=FetchType.LAZY,mappedBy = "a")
B b;
}
public class B {
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "A_ID_FK")
A a;
}
情況是:取A的對象時,就算設置了lazy,結果還是eager馬上獲取的, hibernate打印出的也是多了一條SQL語句;
但當取B的對象時,設置了的lazy就生效了,只有一條SQL語句
查了不少資料,在robin的文章中找到這樣的話:"先來說說Hibernate吧。Hibernate確實功能強悍,但是Hibernate不夠易用,而且有一些明顯的缺陷:one-to-one必須通過bytecode enhancement才能lazy loading",這里說出OneToOne存在這樣的缺陷,然而所說的bytecode enhancement不是很清楚什么意思,猜測是用cglib進行一些對象的動態改變.
在論壇中找到這樣的一段解釋:
Does lazy loading of one-to-one associations work? Lazy loading for
one-to-one associations is sometimes confusing for new Hibernate users.
If you consider one-to-one associations based on shared primary keys
(chapter 7, section 7.1.1, “Shared primary key associations”), an association
can be proxied only if it’s constrained="true". For example, an
Address always has a reference to a User. If this association is nullable
and optional, Hibernate first would have to hit the database to find out
whether a proxy or a null should be applied—the purpose of lazy loading
is to not hit the database at all. You can enable lazy loading through
bytecode instrumentation and interception, which we’ll discuss later.
綜上的原因得出兩種解決方法:
1.將OneToOne改為OneToMany,但幾點是改變了實體關系,對已有代碼也有存在影響
2.將OneToOne的主控方設在你需要lazy loading的實體那里,但兩個都需要lazy loading呢
You can enable lazy loading through
bytecode instrumentation and interception, which we’ll discuss later. 對這句話還要繼續尋找答案