-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Unexpected behavior with net.sf.hibernate.collection.Set
PostPosted: Fri Jul 02, 2004 10:22 am 
Newbie

Joined: Fri Jul 02, 2004 9:53 am
Posts: 3
First, let me say that Hibernate is awesome. I'm just now playing around with it, but really like! I'm seeing one weird behavior though:

Discription: The 'contains' method is returning false, but I know the given object is (the only object) in the set because:

a)It's iterator finds it:

assertEquals(hibernateSet.iterator().next(), givenObject);
//passes

b)Wrapping the hibernate Set in a java.util.HashSet results in 'contains' returning true:

jdkSet = new HashSet(hibernateSet);
assertTrue(jdkSet.contains(givenObject);

Hibernate Version: 2.1.4
Database Version: MySQL, 4.0.20a
mapping documents:

<hibernate-mapping>
<class name="tomcattest.PetOwner" table="pet_owner">
<id name="id" type="string" unsaved-value="null" >
<column name="id" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="name" column="name" type="string"
length="15" not-null="false"/>
<set name="pets" cascade="all" inverse="true" lazy="true">
<key column="pet_owner_id"/>
<one-to-many class="tomcattest.Pet"/>
</set>
</class>
</hibernate-mapping>

<hibernate-mapping>
<class name="tomcattest.Pet" table="pet">

<id name="id" type="string" unsaved-value="null" >
<column name="id" sql-type="char(32)" not-null="false"/>
<generator class="uuid.hex"/>
</id>

<property name="name">
<column name="NAME" length="16" not-null="true"/>
</property>
<property name="sex"/>
<property name="weight"/>
<many-to-one name="owner" class="tomcattest.PetOwner"
column="pet_owner_id"/>

<joined-subclass name="tomcattest.Cat" table="cat">
<key column="id"/>
<property name="outdoorCat" column="is_outdoor"/>
</joined-subclass>
<joined-subclass name="tomcattest.Dog" table="dog">
<key column="id"/>
<property name="huntingDog" column="is_huntingdog"/>
</joined-subclass>

</class>
</hibernate-mapping>

Java Code:
Failing Test:
...
Session session = HibernateUtil.currentSession(testConn);
Transaction tx= session.beginTransaction();

//write some data
PetOwner jenn = new PetOwner();
jenn.setName("Jenn");

Cat mama = new Cat();
mama.setName("Mama Kitty");
mama.setSex('F');
mama.setWeight(8);
mama.setOwner(jenn);
mama.setOutdoorCat(true)
session.saveOrUpdate(jenn);

//retrieve the data
List petOwners = session.find("From PetOwner");
assertEquals(1,petOwners.size());
assertTrue(petOwners.contains(jenn));

Set allPets = ((PetOwner)petOwners.get(0)).getPets();
System.out.println("Broken Set: " + allPets.getClass().getName());
//Above line outputs: net.sf.hibernate.collection.Set
assertEquals(allPets.size(),1);
assertEquals(allPets.iterator().next(), mama);
//Above assertion passes
assertTrue(allPets.contains(mama));
//Above assertion fails, BUG?

tx.rollback();
HibernateUtil.closeSession();
...

Thanks,
Mike


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 02, 2004 12:37 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Most definitely a problem with your equals()/hashCode() implementation for the entities contained in the set.

Keep in mind that Hibernate strictly follows the java Set contract which says, in part, that the value of the elements equals() and hashCode() methods should not change while the object is an element within the Set. If it does change you end up seeing exactly the type of behaviour you are seeing.

Most likely you are basing equals() and hashCode() value on the db identity (i.e., the property mapped with the <id/> element). This is a problem if the entities contained in the set are created as part of a cascade because of there inclusion in a set.


Top
 Profile  
 
 Post subject: Hit the Nail on the Head! Thanks!!!
PostPosted: Tue Jul 06, 2004 9:56 am 
Newbie

Joined: Fri Jul 02, 2004 9:53 am
Posts: 3
Yep, my equals() was including the id attribute [I had only partially implemented hashCode(), since this was just a sample...]. I guess there's a lot of truth to the claim on this website "make sure you really understand hibernate...most of the things that seem like bugs with be due to your own code"! Thanks again...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.