-->
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.  [ 10 posts ] 
Author Message
 Post subject: (obj1 == obj2) is false but (obj1.id == obj2.id)
PostPosted: Fri Oct 21, 2005 3:24 pm 
Beginner
Beginner

Joined: Tue Aug 23, 2005 8:15 am
Posts: 45
I was confused when looked while debugging
two equal objects (they have equal fields and id)
but references to them was not equal!!!

Is it a bug or a feature?
Why Doesn't NHibernate guarantee that obj1 == obj2 if obj1.id == obj2.id.
(both are persisted).

Best regards,
Sergey


Top
 Profile  
 
 Post subject: Re: (obj1 == obj2) is false but (obj1.id == obj2.id)
PostPosted: Fri Oct 21, 2005 4:20 pm 
Regular
Regular

Joined: Mon May 16, 2005 2:15 pm
Posts: 59
sergunok wrote:
Why Doesn't NHibernate guarantee that obj1 == obj2 if obj1.id == obj2.id.
(both are persisted).

Best regards,
Sergey


Perhpas the Equals and GetHashCode overrides aren't correctly created for the objects so NHibernate though they were different objects?

BOb


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 21, 2005 4:22 pm 
Beginner
Beginner

Joined: Wed Jun 01, 2005 3:22 pm
Posts: 38
Location: Menlo Park, CA
This is a fundemental issue you need to understand in .NET. The only way .NET can compare two objects is by reference, unless one or both of the objects implement an appropriate interface like IEquatable<T> (.NET 2.0).

In .NET 1.1, you can override Object.Equals() to give the desired behavior.

You might want to look in to IComparable too.

But be careful when you implement Equals(). What if two objects have the same ID but different data. Are they really equal? The answer is, "It depends."


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 21, 2005 5:02 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
NHibernate actually guarantees this uniqueness, but only for objects belonging to the same session.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 21, 2005 5:18 pm 
Beginner
Beginner

Joined: Wed Jun 01, 2005 3:22 pm
Posts: 38
Location: Menlo Park, CA
Right, but you can load an object from the DB, putting it in the session, and then end up getting an object with the same ID but with different de-serialized from somewhere else (not in the session, right?).

Not a huge, frequent problem, but still something you have to keep in mind.

Also, my "be careful when overriding Equals()" statement was more geared toward .NET in general. Equals() is used in lots of places (like various Contains() and Hashtable lookups etc.) in the .NET Framework, so if you override Equals() you can get some tricky behavior in a few cases.

IIRC, whenever you override Equals(), it's also very strongly suggested that you override GetHashCode() and vice versa.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 22, 2005 5:21 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
rponton wrote:
IIRC, whenever you override Equals(), it's also very strongly suggested that you override GetHashCode() and vice versa.


Yes, two equal objects must have the same HashCode and vice versa...

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 22, 2005 5:57 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
KPixel wrote:
Yes, two equal objects must have the same HashCode and vice versa...


Not vice versa of course! Two objects with equal hash codes are not required to be equal.

rponton wrote:
Right, but you can load an object from the DB, putting it in the session, and then end up getting an object with the same ID but with different de-serialized from somewhere else (not in the session, right?).


Yes, of course that's possible, and you can just create a new object and set it to the same ID, the possibilities are endless :) That's why uniqueness is guaranteed only for objects attached to the same session.


Top
 Profile  
 
 Post subject: you are right!objs were from different sessions.By the way..
PostPosted: Sat Oct 22, 2005 8:03 am 
Beginner
Beginner

Joined: Tue Aug 23, 2005 8:15 am
Posts: 45
what happens when I try to work with object that was loaded from different session and that session is already closed.

can i retrieve lazy attributes of that object?


Top
 Profile  
 
 Post subject: sergey, what does NHibernate when I pass the object as param
PostPosted: Sat Oct 22, 2005 8:22 am 
Beginner
Beginner

Joined: Tue Aug 23, 2005 8:15 am
Posts: 45
of a query?

I mean th following:

can a pass as a param of a query object from OTHER session?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 24, 2005 5:15 am 
Beginner
Beginner

Joined: Mon Oct 03, 2005 4:59 am
Posts: 26
Location: Cambridge, UK
Long threads in the hibernate pages about equals and hashcode can be found here. As far as I can tell it translates directly to NHibernate and C#.


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