criteria = createCriteria("employee");
criteria.add(Restrictions.eq("name", "Jack"));
criteria.createAlias("certificate", "cert");
criteria.add(Restrictions.eq("cert.certType", "MSFT"));
criteriaList = criteria.list();
鉴于下面的数据,我认为上面的查询应该返回一个记录,其中包含一组(设置大小= 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
employee.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>
certificate.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){}
}
修改 如果我将结果(即我的示例中的criteriaList)放在一个集合中,那么它就会删除重复的记录。
Set<Employee> empSet = new HashSet<Employee>(criteriaList);
答案 0 :(得分:3)
我是Hibernate的新手,但遇到类似的问题(父记录被连接重复)
我添加了FetchMode.SUBSELECT
注释(我更喜欢注释)
@OneToMany
@Fetch(FetchMode.SUBSELECT)
在没有重复数据的情况下,它看起来非常适合我。