我在我的數據模型中有OneToOne關係,並且hibernate總是查詢兩個實體以生成結果集。休眠始終運行冗餘查詢
這是數據模型
@Entity
public class C1 {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true, optional = false, targetEntity = C2.class)
private C2 c2;
//... other stuff
}
@Entity
public class C2 extends OtherClassOutOfDomain {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "c2")
private C1 c1;
//... other stuff
}
Hibernate來生成我的架構作爲兩個表和C1表有一個外鍵,C2,什麼是完美的我,我會更經常使用的C1。
BUT
每次我查詢C1休眠產生1個查詢加入每行兩個實體數據,並生成在的結果,每行N次查詢第一個(甚至在我訪問的結果SET)
例如
Hibernate (just one):
select
this_.id as id1_2_1_,
this_.c2_id as authDat22_2_1_,
this_.bio as bio2_2_1_,
this_.blocked as blocked3_2_1_,
this_.commentsAmount as comments4_2_1_,
this_.confirmed as confirme5_2_1_,
this_.deleted as deleted6_2_1_,
c22_.id as id1_0_0_,
c22_.adid as adid2_0_0_,
from
c1 this_
inner join
c2 c22_
on this_.authData_id=c22_.id
Hibernate (N times as the size of previous query):
select
this_.id as id1_2_1_,
this_.c2_id as authDat22_2_1_,
this_.bio as bio2_2_1_,
this_.blocked as blocked3_2_1_,
this_.commentsAmount as comments4_2_1_,
this_.confirmed as confirme5_2_1_,
this_.deleted as deleted6_2_1_,
c22_.id as id1_0_0_,
c22_.adid as adid2_0_0_,
from
c1 this_
inner join
c2 c22_
on this_.authData_id=c22_.id
where
this_.authData_id=?
.....repeat
.....repeat
.....repeat
重複查詢的結果,第一個大的查詢的行中cointained ......有什麼辦法避免這些不必要的請求?我試着設置爲懶惰,但它didn't工作
的代碼我跑得到這個行爲是簡單
HibernateUtils.createNewSession().createCriteria(C1.class).list();
我還沒有訪問結果它觸發嵌套查詢之前
我正在使用休眠5.10 e mysql 5.7.17
可選=假?請試試 –