Restating this more intelligibly.  

Currently:  Proxy hashCode() always is the hashCode() of the identifier.
Proxy equals() defaults to a comparison of identity unless the entity
implements equals() itself, in which case proxies are always
materialized for equals().

This keeps things working (albeit slowly, as all proxies must be
materialized) if entities override equals(), and I'll bet pretty much
anyone that tries to use an entity as a hash key does.  But if the
entity doesn't override equals, everything gets wacky because equals()
is not reflexive:

proxyA.equals(realA) != realA.equals(proxyA)

Try putting these things in a hashset.  Even if you wanted a hashset
based on reference equality (which you must, since you didn't override
equals) you don't get it.

I see that there are really only two useful choices for hibernate
behavior:  allow entities to implement equals() arbitrarily, in which
case proxies must always be materialized, or force entities to implement
equals() based on identity, in which case proxies do not need to be
materialized.  I suspect that the later option is going to cover 99% of
real-world use cases, but someone might grumble so I propose this:

Add a parameter to the .hbm.xml config options that allows specifying
whether or not a proxy should be materialized when equals() is called.
If not, the proxy uses the identifier.

<class name="eg.Cat" table="CATS" proxy="eg.ICat"
materialize-on-equals="false">

Maybe this mechanism should encompass the hashCode() method too, not
sure.

Comments?

I'll be happy to submit a patch for it.

Jeff Schnitzer
[EMAIL PROTECTED]


> -----Original Message-----
> From: Jeff Schnitzer [mailto:[EMAIL PROTECTED]
> Sent: Friday, August 22, 2003 1:18 AM
> To: [EMAIL PROTECTED]
> Subject: [Hibernate] Proxy equals() and hashCode() problem
> 
> I just looked over the LazyInitializer code to manage equals() and
> hashCode() in a proxy and it seems like there's a problem.  I'm trying
> to use an entity as a hash map key without requiring it to be
initialized,
> and the code tries to handle this - except that this behavior is
> disabled if the entity overrides equals.  However, won't it be
> the case that entities (at least ones used as hashmap keys) are
> always going to need to override equals()?  A hashmap on reference
> equality is pretty useless.
> 
> It seems to me that the only practical convention here is to require
that
> entity equals() follow identity rules if it's going to be put in a
> hash map and use proxies.  Otherwise they always have to be
initialized.
> 
> FWIW, my specific use case is an entity which contains a map of
another
> entity to Float.
> 
> Jeff Schnitzer
> [EMAIL PROTECTED]
> 
> 
> -------------------------------------------------------
> This SF.net email is sponsored by: VM Ware
> With VMware you can run multiple operating systems on a single
machine.
> WITHOUT REBOOTING! Mix Linux / Windows / Novell virtual machines
> at the same time. Free trial click
> here:http://www.vmware.com/wl/offer/358/0
> _______________________________________________
> hibernate-devel mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/hibernate-devel


-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel

Reply via email to