How do you implement hashCode?
Consider the case when the object is placed in a Map before it is persisted
and then it is persisted. hashCode should not change.

/Patrik

 

schmeling wrote:
> 
> 
> Patrik Nordwall wrote:
>> 
>> I'm not sure that is a good idea. When an object is created, before it is
>> persisted, it doesn't have an id. When saving the id is assigned, i.e. it
>> is changing. Read more about how we solved it in Sculptor:
>> http://www.fornax-platform.org/cp/display/fornax/3.+Advanced+Tutorial+(CSC)#3.AdvancedTutorial(CSC)-Key
>> 
> That's true, but in the case where the id is null (before it is persisted)
> the equals and hashcode method should use the methods inherited from
> object (entities without an id are equal if they are the same object
> instance).  If we have not overridden the methods at all, one has to
> override them itself in the Impl classes, otherwise these entities with
> surrogate ids cannot be used with Hibernate (which would be a big
> limitation). My proposal is the following implementation in case of the
> surrogate id :
> 
> an example for equals with id as key:
> 
> public boolean equals(Object object)
> {
>         if (object == this)
>             return true;
>         else if (!(object instanceof <Entity>))
>             return false;
>         final <Entity> other = (<Entity>) object;
>         if (this.id == null || other.getId() == null ||
> !this.id.equals(other.getId()))
>             return false;
>         else
>             return true;
>  }
> 
> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Hibernate-Cartridge%28CHB%29-Equals-Hashcode-ToString-tp20830500s17564p20912152.html
Sent from the Fornax-Platform mailing list archive at Nabble.com.


------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
Fornax-developer mailing list
Fornax-developer@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/fornax-developer

Reply via email to