[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-3799) PersistentSet does not honor hashcode/equals contract when loaded eagerly

David Tarico (JIRA) noreply at atlassian.com
Thu Feb 23 14:48:48 EST 2012


    [ https://hibernate.onjira.com/browse/HHH-3799?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=45663#comment-45663 ] 

David Tarico commented on HHH-3799:
-----------------------------------

I think the lack of an ID in an unsaved entity is exactly the problem here.  The book "Java Persistence with Hibernate" by Christian Bauer and Gavin King talks in chapter 9 section 2.2 about implementing equality with a business key instead of the PK since the primary key is not assigned until the object is persisted.    

Like Shawn, we use one or more non-PK fields as the business key in the equals/hashcode so that we can use a set both before and after the objects are persisted and not worry about their identity changing and breaking the contract of a set.  

I think the issue here is that Hibernate simply cannot lazy load the entities in this PersistentSet and must hydrate them before adding them to the set.

Perhaps it should be a configurable option if some users are using the ID and want the lazy loading, but when we've learned from authors on the Hibernate team not use the PK in equals/hashcode and have been doing this practice for many years, this definitely seems like an implementation bug with PersistentSet.

> PersistentSet does not honor hashcode/equals contract when loaded eagerly
> -------------------------------------------------------------------------
>
>                 Key: HHH-3799
>                 URL: https://hibernate.onjira.com/browse/HHH-3799
>             Project: Hibernate ORM
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 3.3.1, 3.5.1
>            Reporter: Igor Vaynberg
>            Assignee: Gail Badner
>         Attachments: test.zip
>
>
> when persistent set is loaded eagerly in some situations it calls hashcode on its items before their field values are populated and then uses this incorrect hashcode to store them in the set. this makes set items inaccessible by any collection items that rely on hashcode such as contains(), remove(), etc.
> attached is a simple maven test project that reproduces the error, unzip and mvn test to run the test.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list