Search code examples
hibernatehibernate-mappinghibernate-criteria

HIbernate returning duplicate or uncompressed objects


I have two entities with oneToMany relationship.

Ex: CollegeEntity collegeId, Set students (OneToMany, fetchType.EAGER)

StudentEntity student_Id college (ManyToOne)

When I query DB using hibernates for a college, its returning me n records of collegeEntity, where n is number of students.

I am expecting result of query to be 1 record of collegeEntity with set of size "n" for students.

I am using Hibernate - 3.6. My query make use of CollegeId in criteria hibernate to get these records. Please guide me what I am doing wrong.

Thanks in advance!


Solution

  • Usually when you get duplicated rows from base entity of a query it is because of the joins you append to that query.

    One way to avoid this behavior is to use DISTINT clause to your query, which woukd remove all duplicated rows of your result. Using criteria using DISTINT can be quite tricky, I recommend you to read about criteria projections distinct, which is what you would need

    Especially using Hibernate it can become really hard to identify the cause of these duplicating rows, so if you find yourself in a such situation you should enable the SQL log generated and then find the source of your problems.