criteria = createCriteria("employee");
criteria.add(Restrictions.eq("name", "Jack"));
criteria.createAlias("certificate", "cert");
criteria.add(Restrictions.eq("cert.certType", "MSFT"));
criteriaList = criteria.list();
鉴于下面的数据,我认为上面的查询应该返回一个包含一组(set size=2)证书的记录,但我得到相同的记录重复两次(证书表中的每条记录一次)。为什么会这样?
员工表:
EMP_ID NAME
123 Jack
111 Mary
000 Larry
证书表数据
emp_id certificate_type seq_no
123 MSFT 1
123 MSFT 2
111 English 1
员工.hbm.xml
<class name="com.Employee" table="Employee" entity-name="employee" mutable="false">
<cache usage="read-only"/>
<id name="id" column="employee_id"/>
<set name="certificate" fetch="select" inverse="true" lazy="false" >
<key column="employee_id" />
<one-to-many class="com.Certificate" entity-name="CertificateType"/>
</set>
</class>
证书.hbm.xml
<class name="com.Certificate" table="Certificate" entity-name="CertificateType" mutable="false">
<cache usage="read-only"/>
<composite-id class="com.usps.nom.tops.model.impl.DispatchLegPKImpl" mapped="true">
<key-property name="empId" column="emp_id" />
<key-property name="seqNo" column="SEQ_NO" />
</composite-id>
<property name="certType" column="certificate_type"/>
</class>
POJO
public class Employee {
private int id;
private String ame;
//getters and setters
public boolean equals(Object obj){}
}
public class Certificate {
private int emp_id;
private String certType;
private String seqNo;
//getters and setters
public boolean equals(Object obj){}
}
编辑: 如果我将结果(即我的示例中的条件列表)放在一个集合中,那么它会删除重复的记录。
Set<Employee> empSet = new HashSet<Employee>(criteriaList);