2013-10-21 66 views
0

我有兩個objects.One是公司,另一種是Country.The公司對象有一些屬性,其中兩個與國家目標,這被稱爲「controllerCountry」和「registCountry」有關。當我使用Hibernate Criteria來獲取公司時,有一個n + 1個查詢(不是真的)問題。使用Hibernate的標準來獲取相同的對象

我公司的目標代碼:

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「左」一起拿到「controllerCountry」和「registCountry」)。我不明白爲什麼還有其他5 SQL。如果問題是n + 1查詢,應該有其他10個sql(5個sql用於選擇'controllerCountry',另一個用於選擇'registCountry')。有沒有人可以告訴我發生了什麼以及如何才能得到結果SQL。

回答

0

我想你需要的是預先加載,你可以用下面的操作:對於其他集合

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

與相同。

+0

謝謝你。但不幸的是,它不起作用。 – Jie

0

這是因爲在第一次查詢JOIN針對標準和WHERE子句可選匹配執行。你必須指示Criteria立即取回兒童。你可以通過。至於未來,這裏是不錯的一塊Hibernate's documentation

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