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

Shawn Clowater (JIRA) noreply at atlassian.com
Thu Feb 23 14:38:48 EST 2012


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

Shawn Clowater commented on HHH-3799:
-------------------------------------

It's not directly the same but it's related to saying that using the PK would be the silver bullet.  Simple test case demonstrating 2 of the issues off the top of my head that would cause grief with using a generated id.  You would be forced to save the child before adding it to the parent in those cases.  (if just using the PK as the hashcode/equals)

{code}    
    @Test
    public void cantAddMultipleUnsaved(){
        B b = new B();
        b.as.add(new A());
        b.as.add(new A());
        b.as.add(new A());
        b.as.add(new A());

        //fails - size is only 1 since unsaved entities hashed to the same value
        Assert.assertEquals(4, b.as.size());
    }
    
    @Test
    public void saveThenCantRemove(){
        B b = new B();
        final A a = new A();
        b.as.add(a);

        Assert.assertEquals(1, b.as.size());
        
        b.as.remove(b);
        Assert.assertEquals(0, b.as.size());
        
        b.as.add(a);
        Assert.assertEquals(1, b.as.size());
        
        //pretend we have a session and save at this point
        a.id = (long) 8675309;
        
        b.as.remove(a);
        
        //fails since the hashcode changed
        Assert.assertEquals(0, b.as.size());
    }
    
    public class A{
        public Long id;

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof A)) return false;

            A a = (A) o;

            if (id != null ? !id.equals(a.id) : a.id != null) return false;

            return true;
        }

        @Override
        public int hashCode() {
            return id != null ? id.hashCode() : 0;
        }
    }

    public class B extends A{
        public Set<A> as = new HashSet<A>();
    }
{code}

> 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