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

Gail Badner (JIRA) noreply at atlassian.com
Wed Apr 21 18:14:34 EDT 2010


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-3799?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=36594#action_36594 ] 

Gail Badner commented on HHH-3799:
----------------------------------

As far as I can tell, this issue can only come up when a non-lazy (eagerly loaded) collection contains entities of a class that overrides Object.hashCode().

You should not see this problem if you don't override Object.hashCode(). 

For a discussion of when Hibernate requires equals() and hashCode() to be overridden, see
http://docs.jboss.org/hibernate/stable/core/reference/en/html/persistent-classes.html#persistent-classes-equalshashcode . It's worth mentioning that, if Hibernate does not require hashCode() and equals() to be overridden and your application does not specifically require it, you may not need to override them at all.

I don't think it will be easy to come up with a fix that will work in all cases.

I think it would be fairly straightforward to fix this issue for cases where hashCode() only depends on simple (non-association) properties.

To illustrate a case where the fix would work, suppose:
- A has a non-lazy, one-to-many association with B, a.bs
- B has a non-lazy, many-to-one association with A, b.a
- B has a simple string property, name
- B overrides Object.hashCode()
- B.hashCode() uses B.name to calculate the hash code; B.hashCode() does not use B.a in the calculation

Basically, the fix I am proposing would be to initialize simple properties in a loading entity before initializing its associations. I'll provide more details about this partial fix and why it would work in another comment.

I'd like to get an idea about how people are implementing hashCode(). If this would work for enough people, it might be worthwhile to implement this partial fix.

Please let us know if your implementations of hashCode() only use simple (non-association) properties.

> PersistentSet does not honor hashcode/equals contract when loaded eagerly
> -------------------------------------------------------------------------
>
>                 Key: HHH-3799
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-3799
>             Project: Hibernate Core
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 3.3.1, 3.5.1
>            Reporter: Igor Vaynberg
>            Assignee: Gail Badner
>             Fix For: 3.5.x
>
>         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.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list