John Thomas commented on Bug HHH-6783

I tend to agree that this is a bug. I also spent nearly a day trying to work out what was going on. I am using JPA2.0 annotation/objects with Hibernate 4.1.4. My example is as follows:

  • Employee entity with a ManyToOne relationship with a Schedule Entity. * The Employee entity also has a OneToMany relationship with a Qualification Entity.
  • The Schedule entity has an ElementCollection (java type List) of Embeddable type ScheduleEntry (e.g. start/stop time).
  • The ElementCollection is set for Eager loading.

Let's assume I have an employee record with four qualification entries and a Schedule with a single ScheduleEntry record. When I load the Employee object via EntityManager.findById, Hibernate will issue a single SQL SELECT joining the results, thus retrieving four rows. Since the ScheduleEntry table has no unique identifier, hibernate will populate the List in the Schedule object with 4 copies of the ScheduleEntry.

I fail to see any situation where this is desirable and in all situations it seems to be the wrong result. Changing between lazy and eager fetching should not result in a user being delivered an object with entirely different content, and in the latter case, the wrong content.

I also don't see why it is required. All of the information is available for hibernate to detect this situation, and when it exists, use two SQL queries. As far as I am aware, EAGER doesn't require a single SQL request be used, just that all fields should be populated during the find call.

I understand that using a Set will solve this, but that seems like a hack at the user level. And I can see situations where multiple copies of identical objects might be required.

This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira