我遇到了hibernate生成的HSQL语句的问题,其中,当获取实体及其一对多的对应项时,Select查询具有DUPLICATE列。我的类定义如下所示;
@javax.persistence.Entity
@Table(name = "SCHOOL")
public Class School implements Serializable {
@Id
private long id;
@Column(name="NAME")
private String name;
@Column(name="ZIP_CODE")
private String zipCode;
@OneToMany
@JoinColumn(name="NAME", referencedColumnName="SCHOOL_NAME")
private Set<Student> students= new HashSet<Student>();
/*
*
*followed by other variable definitions, getters and setters
*/
}
@javax.persistence.Entity
@Table(name = "STUDENT")
public Class Student implements Serializable {
@Id
private long id;
@Column(name="FNAME")
private String fName;
@Column(name="LNAME")
private String lName;
@Column(name="SCHOOL_NAME")
private String schoolName;
@Column(name="DOB")
private Date dateOfBirth;
/*
*
*followed by other variable definitions, getters and setters
*/
}
我有以下标准,以特定的邮政编码取得学校和相应的学生。
Criteria criteria = session.createCriteria(School.class);
criteria.add(Restrictions.in("zipCode", 56789));
List<School> schools = criteria.list();
以上条件是在后台生成以下HSQL,并在SELECT查询中为STUDENT列提供重复的列条目。
select
this_.ZIP_CODE as ZIP_CODE1_16_3_,
this_.name as NAME_16_3_,
this_.LICENSE_NUM as LICENSE_N3_16_3_,
student3_.FNAME as ZIP_CODE1_16_5_,
student3_.LNAME as NAME_16_5_,
student3_.GRADE as GRADE4_16_5_,
student3_.ETHNICITY as ETHNICITY5_16_5_,
student3_.SCHOOL_NAME as SCHOOL_NA6_16_5_,
student3_.FNAME as ZIP_CODE1_14_5_,
student3_.LNAME as NAME_14_5_,
student3_.GRADE as GRADE4_14_5_,
student3_.ETHNICITY as ETHNICITY5_14_5_,
student3_.SCHOOL_NAME as SCHOOL_NA6_14_5_,
student3_.FNAME as ZIP_CODE1_14_1_,
student3_.LNAME as NAME_14_1_,
student3_.GRADE as GRADE4_14_1_,
student3_.ETHNICITY as ETHNICITY5_14_1_,
student3_.SCHOOL_NAME as SCHOOL_NA6_14_1_
from
SCHOOL this_,
STUDENT student3_
where
and this_.name=student3_.SCHOOL_NAME(+)
and this_.ZIP_CODE=56789;
(上面的查询在此处发布之前已稍微调整过一点。)
请建议一个解决方案,我可以使用它解决SELECT查询中的重复列。
注意:我使用的是hibernate 4.2.7和hibernate-jpa-2.0-api