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.  [ 1 post ] 
Author Message
 Post subject: Proxies get initialized when you override Equals()
PostPosted: Thu Jan 31, 2008 1:31 pm 
Regular
Regular

Joined: Tue Dec 02, 2003 6:25 pm
Posts: 61
Location: Dallas, TX
Hibernate version: 1.2.1GA

Just noticed this and am wondering (a) if it's a bug or design flaw and (b) what is the best way to handle it.

NH proxies seem to be pretty good about not unnecessarily initializing themselves. You can get an entity's Id and invoke Equals() and GetHashCode(), all without initializing.

UNLESS you override Equals(), as is recommended by the NH docs, along with GetHashCode().

Once you do that, any call to Equals() or GetHashCode() results in the initialization of the object. To me, this suggests that there's no good way to get proxies to play nicely with .NET collections.

It seems like my choices are:
  • Override Equals(), allowing me to correctly insert and retrieve entries from, say, an IDictionary(), but automatically initializing everything I put into it
  • Don't override anything, meaning I risk the reliability of operations on the IDictionary, but at least I don't unnecesarily initialize anything
  • Write my own collection implementations or object wrappers.


If I assume that (1) my session will always give back the same proxy for a given instance, and (2) NHibernate's own collection implementations don't rely on me overriding Equals(), and I also don't use collections that span sessions, maybe the lesser evil is the second option I listed.

For the moment, I'm creating an NHibernate.Engine.EntityKey to put entities into an IDictionary, and that seems to work fine, but I'd rather just put the entity/proxy itself into the IDictionary and rely on normal collection semantics.


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

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.