简体   繁体   中英

Hibernate @OneToMany relationship

I have some trouble with @OneToMany relationship. First entity represents some kind of goods.

@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;
}

Second are being grouping of goods

@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;
}

When I make a select like "select list from AR_GROUP list". Hibernate for each group create a select,why it do not use outer join? Or it is not possible.

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=?

Thanks for reply.

Multiple running queries could be because of the n + 1 issue.

Use JOIN FETCH to fetch the parent and all the mapped child entities in one query.

Check out this similar answer.

通过使用hql join fetch解决了问题,在我的选项中选择如下所示

from AR_GROUP list left outer join fetch list.artiqules

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM