0

我有两个对象。一个是 Company,另一个是 Country。Company 对象有一些属性,其中两个与 Country 对象相关,分别称为“controllerCountry”和“registCountry”。当我使用 Hibernate Criteria 获取 Companies 时,有一个 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()

我从控制台获得选择 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 查询,应该还有其他 10 个 sql(5 个 sql 用于 select 'controllerCountry',另一个用于 select 'registCountry')。谁能告诉我发生了什么,我怎样才能得到结果sql。

4

2 回答 2

0

我认为您需要的是急切加载,您可以通过以下方式进行:

@ManyToOne(targetEntity = Country.class, fetch=FetchType.EAGER)
@JoinColumn(name="controller_country",referencedColumnName="code",insertable=false,updatable=false)
public Country getControllerCountryArea() {
    return controllerCountryArea;
}

其他系列也是如此。

于 2013-10-21T06:03:50.703 回答
0

这是因为在第一个查询JOIN中执行条件和可选匹配WHERE子句。你必须立即指挥Criteria去接孩子。你可以这样做。至于未来,这里是Hibernate 的文档

criteria.setFetchMode("name_of_property_to_fetch", FetchMode.EAGER);
于 2013-10-25T05:47:36.537 回答