私はデータモデル内でOneToOne関係を持っており、hibernateは常に結果セットを生成するために両方のエンティティをクエリしています。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テーブルとして私のスキーマを生成されたが、私はC1を使用して、より頻繁になりますように私に最適です何を、C2への外部キーを持っています。私はC1の照会
BUT
毎回行あたり両方のエンティティのデータを結合する1つのクエリを生成し、第1の結果のすべての行のN個のクエリを生成休止状態(I、結果セットにアクセスする前であっても)
例
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
オプション= falseの?本当に試してください –