Hibernate @OneToMany关系

时间:2017-05-29 13:33:10

标签: java hibernate

我对@OneToMany关系有些麻烦。 第一实体代表某种商品。

@Entity(name = "ARTIQULE")
public final class ArtiquleEntity extends CustomEntity{

@Id
@Column(name = "ID")
public Long id;

@NotNull
@Column(name = "METRIC_ID")
public Long metricId;

@NotNull
@Column(name = "STATUS_ID")
public Long statusId;

@NotNull
@Column(name = "NAME")
public String name;

@NotNull
@Column(name = "SHORT_NAME")
public String shortName;

@NotNull
@ManyToOne
@JoinColumn(name = "GROUP_ID")
public ArtGroupEntity artGroup;
}

其次是商品分组

@Entity(name = "AR_GROUP")
public final class ArtGroupEntity extends CustomEntity {

@Id
@Column(name = "ID")
public Long id;

@NotNull
@Column(name = "GROUP_ID")
public Long groupId;

@NotNull
@Column(name = "STATUS_ID")
public Long statusId;

@NotNull
@Column(name = "NAME")
public String name;

@NotNull
@Column(name = "L")
public Integer left;

@NotNull
@Column(name = "R")
public Integer right;

@NotNull
@OneToMany(mappedBy = "artGroup", fetch = FetchType.EAGER)
public List<ArtiquleEntity> artiquleEntities;
}

当我选择&#34;从AR_GROUP列表中选择列表&#34;。 Hibernate为每个组创建一个select,为什么它不使用外连接? 或者这是不可能的。

Hibernate: select artgroupen0_.ID as ID1_0_, artgroupen0_.GROUP_ID as GROUP_ID2_0_, artgroupen0_.L as L3_0_, artgroupen0_.NAME as NAME4_0_, artgroupen0_.R as R5_0_, artgroupen0_.STATUS_ID as STATUS_I6_0_ from AR_GROUP artgroupen0_
Hibernate: select artiquleen0_.GROUP_ID as GROUP_ID2_1_0_, artiquleen0_.ID as ID1_1_0_, artiquleen0_.ID as ID1_1_1_, artiquleen0_.GROUP_ID as GROUP_ID2_1_1_, artiquleen0_.METRIC_ID as METRIC_I3_1_1_, artiquleen0_.NAME as NAME4_1_1_, artiquleen0_.SHORT_NAME as SHORT_NA5_1_1_, artiquleen0_.STATUS_ID as STATUS_I6_1_1_ from ARTIQULE artiquleen0_ where artiquleen0_.GROUP_ID=?
Hibernate: select artiquleen0_.GROUP_ID as GROUP_ID2_1_0_, artiquleen0_.ID as ID1_1_0_, artiquleen0_.ID as ID1_1_1_, artiquleen0_.GROUP_ID as GROUP_ID2_1_1_, artiquleen0_.METRIC_ID as METRIC_I3_1_1_, artiquleen0_.NAME as NAME4_1_1_, artiquleen0_.SHORT_NAME as SHORT_NA5_1_1_, artiquleen0_.STATUS_ID as STATUS_I6_1_1_ from ARTIQULE artiquleen0_ where artiquleen0_.GROUP_ID=?
Hibernate: select artiquleen0_.GROUP_ID as GROUP_ID2_1_0_, artiquleen0_.ID as ID1_1_0_, artiquleen0_.ID as ID1_1_1_, artiquleen0_.GROUP_ID as GROUP_ID2_1_1_, artiquleen0_.METRIC_ID as METRIC_I3_1_1_, artiquleen0_.NAME as NAME4_1_1_, artiquleen0_.SHORT_NAME as SHORT_NA5_1_1_, artiquleen0_.STATUS_ID as STATUS_I6_1_1_ from ARTIQULE artiquleen0_ where artiquleen0_.GROUP_ID=?

感谢您的回复。

2 个答案:

答案 0 :(得分:0)

多个正在运行的查询可能是因为n + 1问题。

使用JOIN FETCH在一个查询中获取父项和所有映射的子实体。

查看this类似的答案。

答案 1 :(得分:0)

使用hql join fetch解决问题, 在我的选项中选择看起来像这样

from AR_GROUP list left outer join fetch list.artiqules