-->
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.  [ 4 posts ] 
Author Message
 Post subject: PersistentGenericSet<T>.Contains() - Possible Bug?
PostPosted: Wed Apr 18, 2007 9:36 pm 
Newbie

Joined: Sat Jun 11, 2005 1:56 am
Posts: 19
Hibernate version: 1.2RC1
Name and version of the database you are using:SQL 2005

Hi All,

I'm getting very strange behaviours with sets. This will be much easier to explain with a 'Unit Test' style code...

Entity e = ....; // Entity in the session
ChildEntity ce = new ChildEntity(e, "_Prob").Save();
// ChildEntity adds itself to Entitie's ChieldEntities ISet.
// Lets verify this is the case.
IEnumerator enu = c.ChildEntities.GetEnumerator();
enu.MoveNext();
ChildEntity verifyInParent = enu.Current; // This just verifies that ChildEntity added itself appropriatelly to Entity's ChildEntities Set

Assert.AreEqual(verifyInParent , ce); // .Equals working file
Assert.IsTrue(verifyInParent == ce); // Even same reference
Assert.AreEqual(verifyInParent .GetHashCode(), ce.GetHashCode());
// Hashcodes seem ok
Assert.IsTrue(c.ProbabilityEntities.Contains(pInList)); // Fail Test

Is there something I'm missing here? The code to add the ChildEntity to the Parents ChildEntities Set is not complex, In fact its a direct call to ChildEntities.Add(ce).

Any help would be greatly appreciated.

Cheers,

Guido Tapia


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 19, 2007 12:29 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Check that your hash code doesn't change between the time you add the entity to the set and the time you call Contains.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 19, 2007 1:36 am 
Newbie

Joined: Sat Jun 11, 2005 1:56 am
Posts: 19
So, I assume this is because hashcodes are cached for efficiency.

What happens when the hashcode has to change after being added to the collection?

Eg:

Entity e;
ChildEntity ce;

c.ChildEntities.Add(ce);
ce.Property = 'Test'; // Change value of a field. Hence the hashcode.
c.ChildEntities.Contains(ce); // Will be false??

Is there a way I can reset the cached hashcodes? So I can use .Contains?

Thanks heaps,

Guido Tapia


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 19, 2007 4:52 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
It has nothing to do with caching. A hash set stores items in buckets according to their hash code values (that's why it's called hash set, after all). So if the hash code of an item changes while in the set, the set will look for it in a wrong place and won't find the item.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.