使用Hibernate Criteria获取相同的对象

时间:2013-10-21 04:59:18

标签: java hibernate

我有两个对象。一个是公司,另一个是Country.Company对象有一些属性,其中两个与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查询,那么应该有其他10个sql(5个用于选择'controllerCountry'的sql,另一个用于select'registCountry')。有人可以告诉我发生了什么,我怎样才能得到结果SQL。

2 个答案:

答案 0 :(得分:0)

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

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

其他系列也一样。

答案 1 :(得分:0)

这是因为在JOIN子句中执行标准和可选匹配的第一个查询WHERE。您必须指示Criteria立即取回儿童。你可以做到这一点。至于未来,这里有Hibernate's documentation

criteria.setFetchMode("name_of_property_to_fetch", FetchMode.EAGER);