Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Hibernate条件获取相同的对象_Java_Hibernate - Fatal编程技术网

Java 使用Hibernate条件获取相同的对象

Java 使用Hibernate条件获取相同的对象,java,hibernate,Java,Hibernate,我有两个对象。一个是Company,另一个是Country。Company对象有一些属性,其中两个与Country对象相关,称为“controllerCountry”和“RegistrCountry”。当我使用Hibernate条件获取公司时,有一个n+1查询(实际上不是)问题 我的公司目标代码: public class Company implements Serializable{ private Country controllerCountryArea; private Count

我有两个对象。一个是Company,另一个是Country。Company对象有一些属性,其中两个与Country对象相关,称为“controllerCountry”和“RegistrCountry”。当我使用Hibernate条件获取公司时,有一个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;
}
}
我的国家/地区目标代码:

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'和'RegistrCountry')。我不明白为什么还有其他5个sql。如果问题是n+1-query,则应该有其他10个sql(5个sql用于选择“controllerCountry”,另一个用于选择“RegistrCountry”)。有人能告诉我发生了什么,以及如何通过一个sql获得结果。

我想您需要的是快速加载,您可以使用以下操作:

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

另一个集合也是如此。

这是因为在第一个查询中,
WHERE
子句中的条件和可选匹配执行JOIN
。您必须指示
条件
立即取孩子。你可以这样做。至于未来,这是一个很好的例子