Hibernate n+1使用@ElementCollection和@CollectionTable进行选择

Hibernate n+1使用@ElementCollection和@CollectionTable进行选择,hibernate,jpa,Hibernate,Jpa,我已经为如下要求编写了一个本机查询: @Data @ToString @Entity(name = "tableX") public class X implements Serializable { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "x_seq") @SequenceGenerator(allocationSize = 1, name =

我已经为如下要求编写了一个本机查询:

@Data
@ToString
@Entity(name = "tableX")
public class X implements Serializable {

  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "x_seq")
  @SequenceGenerator(allocationSize = 1, name = "x_seq", sequenceName = "x_seq")
  @Column(name = "id")
  private Long id;
 
  @ElementCollection
  @CollectionTable(name = "x_y", joinColumns = @JoinColumn(name = "x_id", nullable = false))
  @Column(name = "x_y_id")
  private Set<Long> xYs;

  @ElementCollection
  @CollectionTable(name = "x_Z", joinColumns = @JoinColumn(name = "x_id", nullable = false))
  @Column(name = "x_Z_id")xYIds;

  @ElementCollection
  @CollectionTable(name = "x_A",  joinColumns = @JoinColumn(name = "x_id", nullable = false))
  @Column(name = "x_A_id")
  private Set<Long> xAIds;

  ...

}
这将导致以下查询

Hibernate: select * from tableX where someId in (<id List>) order by created_on desc <More criteria may follow>

我可以有一个更好的方法来获取这些,我将需要这个信息肯定。我不确定FetchType.EAGER将如何处理这个问题,但我有兴趣以某种方式进行单个查询。改变结构现在还不能确定,我希望通过减少这些查询来实现良好的优化。蒂亚

注:由于隐私原因,字段被屏蔽,请注意提供的数据。蒂亚

Hibernate: select * from tableX where someId in (<someIds>) order by created_on desc
Hibernate: select xA0_.x_id as x_id1_4_0_, xA0_.x_y_id as x_wo2_4_0_ from x_y xA0_ where xA0_.x_id=?
Hibernate: select xY0_.x_id as x_id1_3_0_, xY0_.x_Yt_pool_id as x_ti2_3_0_ from x_Yt_pool xY0_ where xY0_.x_id=?
Hibernate: select xY0_.x_id as x_id1_0_0_, xY0_.x_Y_id as x_in2_0_0_ from x_Y xY0_ where xY0_.x_id=?
Hibernate: select z0_.x_id as x_id1_1_0_, z0_.zs as z2_1_0_ from x_z z0_ where z0_.x_id=?
Hibernate: select xA0_.x_id as x_id1_4_0_, xA0_.x_y_id as x_wo2_4_0_ from x_y xA0_ where xA0_.x_id=?
Hibernate: select xY0_.x_id as x_id1_3_0_, xY0_.x_Yt_pool_id as x_ti2_3_0_ from x_Yt_pool xY0_ where xY0_.x_id=?
Hibernate: select xY0_.x_id as x_id1_0_0_, xY0_.x_Y_id as x_in2_0_0_ from x_Y xY0_ where xY0_.x_id=?
Hibernate: select z0_.x_id as x_id1_1_0_, z0_.zs as z2_1_0_ from x_z z0_ where z0_.x_id=?
Hibernate: select xA0_.x_id as x_id1_4_0_, xA0_.x_y_id as x_wo2_4_0_ from x_y xA0_ where xA0_.x_id=?
Hibernate: select xY0_.x_id as x_id1_3_0_, xY0_.x_Yt_pool_id as x_ti2_3_0_ from x_Yt_pool xY0_ where xY0_.x_id=?
Hibernate: select xY0_.x_id as x_id1_0_0_, xY0_.x_Y_id as x_in2_0_0_ from x_Y xY0_ where xY0_.x_id=?
Hibernate: select z0_.x_id as x_id1_1_0_, z0_.zs as z2_1_0_ from x_z z0_ where z0_.x_id=?
Hibernate: select xA0_.x_id as x_id1_4_0_, xA0_.x_y_id as x_wo2_4_0_ from x_y xA0_ where xA0_.x_id=?
Hibernate: select xY0_.x_id as x_id1_3_0_, xY0_.x_Yt_pool_id as x_ti2_3_0_ from x_Yt_pool xY0_ where xY0_.x_id=?
Hibernate: select xY0_.x_id as x_id1_0_0_, xY0_.x_Y_id as x_in2_0_0_ from x_Y xY0_ where xY0_.x_id=?
Hibernate: select z0_.x_id as x_id1_1_0_, z0_.zs as z2_1_0_ from x_z z0_ where z0_.x_id=?
Hibernate: select xA0_.x_id as x_id1_4_0_, xA0_.x_y_id as x_wo2_4_0_ from x_y xA0_ where xA0_.x_id=?
Hibernate: select xY0_.x_id as x_id1_3_0_, xY0_.x_Yt_pool_id as x_ti2_3_0_ from x_Yt_pool xY0_ where xY0_.x_id=?
Hibernate: select xY0_.x_id as x_id1_0_0_, xY0_.x_Y_id as x_in2_0_0_ from x_Y xY0_ where xY0_.x_id=?
Hibernate: select z0_.x_id as x_id1_1_0_, z0_.zs as z2_1_0_ from x_z z0_ where z0_.x_id=?