选择没有N + 1个查询批处理的@OneToOne实体?

问题描述

因此,请设想以下情况...我们有两个不同的实体,它们与@OneToOne注释相关联-

@javax.persistence.Entity
@Table(name = "spawner")
@Access(value = AccessType.FIELD)
public class Spawner extends HibernateComponent {

    @OneToOne
    @JoinColumn(name = "location_id",referencedColumnName = "identity_id")
    @Fetch(FetchMode.JOIN)
    public Location location;

    public Spawner() { }
@Entity
@Table(name = "location")
@Access(AccessType.FIELD)
public class Location extends HibernateComponent {

    @ManyToOne
    @JoinColumn(name = "chunk_id",referencedColumnName = "identity_id",updatable = false)
    @Fetch(FetchMode.JOIN)
    public Chunk chunk;

    public Location() {}
}

当我们使用以下语句Spawner选择session.createQuery("select s from Spawner s where s in (...)");时,休眠将生成如下所示的输出。

Hibernate: select identity0_.id as id1_2_0_,location1_.identity_id as identity1_6_1_,identity0_.tag as tag2_2_0_,identity0_.typeID as typeid3_2_0_,location1_.chunk_id as chunk_id4_6_1_,location1_.x as x2_6_1_,location1_.y as y3_6_1_ from identity identity0_ inner join location location1_ on (location1_.identity_id=identity0_.id and (identity0_.id in (8326589099709645653,6287325594386187920,5407611297503526929,8523982519594665733,4028725686660451036,4729790674605130415,6901230747306707572,89683718271946391,663547951024983400,487871300966958647,2986281183600263327,7129934223659254130,7423461182852450838,6258927419235452915,4756503673250530721,7989242675644875262,7502164321920434546,9125201177335319448,5276750027792075277,2958385004616501743,8131213851438696494,5378786184839581257,4800722432645154706)))
Hibernate: select chunk0_.identity_id as identity1_0_0_,chunk0_.createdOn as createdo2_0_0_,chunk0_.x as x3_0_0_,chunk0_.y as y4_0_0_,inchunk1_.Chunk_identity_id as chunk_id1_1_1_,identity2_.id as inchunk_2_1_1_,identity2_.id as id1_2_2_,identity2_.tag as tag2_2_2_,identity2_.typeID as typeid3_2_2_ from chunk chunk0_ left outer join chunk_identity inchunk1_ on chunk0_.identity_id=inchunk1_.Chunk_identity_id left outer join identity identity2_ on inchunk1_.inChunk_id=identity2_.id where chunk0_.identity_id=?
Hibernate: select identity0_.id as id1_2_0_,identity0_.typeID as typeid3_2_0_ from identity identity0_ where identity0_.id=?
Hibernate: select chunk0_.identity_id as identity1_0_0_,identity0_.typeID as typeid3_2_0_ from identity identity0_ where identity0_.id=?

如您所见,hibernate遇到n + 1问题,并为每个加载的位置执行一个“选择块查询” ...

我们如何防止这种情况,并迫使他们将其批处理/合并为更少的正在执行的查询?我们仍然希望该“位置”引用以及“大块”引用能尽快加载...因此,在加载“生成器”时我们不想忽略它们,相反,我们的目标是对要执行的查询进行批处理以选择“位置”和位置中的“块”。

对此有何想法?我们如何在休眠状态下实现这一目标?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)