2 つのオブジェクトがあります。1 つは Company で、もう 1 つは Country です。Company オブジェクトにはいくつかのプロパティがあり、そのうちの 2 つは Country オブジェクトに関連しており、「controllerCountry」と「registCountry」と呼ばれます。Hibernate Criteria を使用して会社を取得すると、 n + 1クエリ(実際にはそうではない)の問題があります。
私の会社のオブジェクト コード:
public class Company implements Serializable{
private Country controllerCountryArea;
private Country registCountryArea;
@ManyToOne(targetEntity = Country.class,optional=true)
@JoinColumn(name="controller_country",referencedColumnName="code",insertable=false,updatable=false)
@LazyToOne(LazyToOneOption.PROXY)
public Country getControllerCountryArea() {
return controllerCountryArea;
}
public void setControllerCountryArea(Country controllerCountryArea) {
this.controllerCountryArea = controllerCountryArea;
}
@ManyToOne(targetEntity = Country.class,optional=true)
@JoinColumn(name="regist_country",referencedColumnName="code",insertable=false,upda table=false)
@LazyToOne(LazyToOneOption.PROXY)
public Country getRegistCountryArea() {
return registCountryArea;
}
public void setRegistCountryArea(Country registCountryArea) {
this.registCountryArea = registCountryArea;
}
}
私の国のオブジェクト コード:
public class Country implements Serializable {
private static final long serialVersionUID = 3070416090656703733L;
private Integer id;
private String code;
private String numcode;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name="CODE")
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Column(name="NUMCODE")
public String getNumcode() {
return numcode;
}
public void setNumcode(String numcode) {
this.numcode = numcode;
}
}
私のクエリコード:
Criteria criteria = this.getSession().createCriteria(Company.class);
List list = criteria.list()
コンソールから select SQL を取得します。
Hibernate: select this_.id as id14_2_, this_.controller_country as controller10_14_2_, this_.regist_country as regist28_14_2_, countryare2_.id as id4_0_, countryare2_.CODE as CODE4_0_, countryare2_.NUMCODE as NUMCODE4_0_, countryare3_.id as id4_1_, countryare3_.CODE as CODE4_1_, countryare3_.NUMCODE as NUMCODE4_1_ from ps_ctf_company this_ left outer join PS_CTF_DICT_country countryare2_ on this_.controller_country=countryare2_.CODE left outer join PS_CTF_DICT_country countryare3_ on this_.regist_country=countryare3_.CODE order by this_.code asc
Hibernate: select countryare0_.id as id4_0_, countryare0_.CODE as CODE4_0_, countryare0_.NUMCODE as NUMCODE4_0_ from PS_CTF_DICT_country countryare0_ where countryare0_.CODE=?
Hibernate: select countryare0_.id as id4_0_, countryare0_.CODE as CODE4_0_, countryare0_.NUMCODE as NUMCODE4_0_ from PS_CTF_DICT_country countryare0_ where countryare0_.CODE=?
Hibernate: select countryare0_.id as id4_0_, countryare0_.CODE as CODE4_0_, countryare0_.NUMCODE as NUMCODE4_0_ from PS_CTF_DICT_country countryare0_ where countryare0_.CODE=?
Hibernate: select countryare0_.id as id4_0_, countryare0_.CODE as CODE4_0_, countryare0_.NUMCODE as NUMCODE4_0_ from PS_CTF_DICT_country countryare0_ where countryare0_.CODE=?
Hibernate: select countryare0_.id as id4_0_, countryare0_.CODE as CODE4_0_, countryare0_.NUMCODE as NUMCODE4_0_ from PS_CTF_DICT_country countryare0_ where countryare0_.CODE=?
実際、最初の sql は私が望むものであり、期待どおりに 5 つの結果が得られます (ues 'left join' は 'controllerCountry' と 'registCountry' を取得します)。他の 5 つの sql がある理由がわかりません。問題が n+1-query の場合、他に 10 個の sql (select 'controllerCountry' 用に 5 個の sql、select 'registCountry' 用にもう 1 個の sql) があるはずです。 SQL。