0
criteria = createCriteria("employee");
criteria.add(Restrictions.eq("name", "Jack"));
criteria.createAlias("certificate", "cert");
criteria.add(Restrictions.eq("cert.certType", "MSFT"));  

criteriaList = criteria.list();

Given the data below, I think the query above should have returned one record that contains a set(set size=2) of certificates but I get the same record duplicated twice(once for each record in Certificate table). Why is this happening?

Employee Table:

EMP_ID          NAME    
123             Jack                                
111             Mary            
000             Larry   

Certificate table data

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>

POJOs

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){}
    }

EDIT: If I put the result (ie criteriaList in my example) in a set, then it gets rid of the duplicate record.

Set<Employee> empSet = new HashSet<Employee>(criteriaList); 
5
  • 1
    Not exactly sure, but can you just try using FetchMode in this case? and try using SELECT instead of JOIN. Commented Oct 25, 2013 at 17:07
  • what is seq_no? It seems you have duplicate entries/associates in database. Commented Oct 25, 2013 at 17:14
  • @Nambari This is a dummy data that I created for this question. Please ignore the duplicate entries in the table. Thank you. Commented Oct 25, 2013 at 17:15
  • 1
    No, what I mean is, it seems because of data you have in table and corresponding joins, you are seeing duplicate entries. I would suggest enable show_sql and see what is the query being executed. Hibernate is complex, wrong mappings may results in unnecessary stuff. Commented Oct 25, 2013 at 17:21
  • @R.J My employee mapping file has fetch="select". I am new to hibernate but according to this doc they're equivalent docs.jboss.org/hibernate/core/3.3/api/org/hibernate/… Commented Oct 25, 2013 at 18:05

1 Answer 1

3

I'm newbie in Hibernate, but faced with similar problem (parent records are duplicated by join)

I have added FetchMode.SUBSELECT annotation (I prefer annotations)

@OneToMany
@Fetch(FetchMode.SUBSELECT)

It looks like working perfectly for me without duplicating data.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.