You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Gavin Bong <ga...@gmail.com> on 2007/10/21 08:26:38 UTC

hashCode/equals for entities

Hi,

When implementing the hashCode and equals for an entity, do
I need to include my generated id in the calculation ?

e.g

public class EventModel
    implements Serializable
{

    @Id
    @Column( name = "id" )
    @GeneratedValue
    private String id;

    ... snip
}

The other non-null members are already in hashCode & equals.
Do I include the "id" above as well ?

Thanks.

-- 
Regards,

Gavin Bong (Jayway Malaysia)
blogging at http://raverun.com/jayway

-----
Ce qui compte dans une vie,
c'est l'intensité d'une vie,
ce n'est pas la durée d'une vie
~ Jacque Brel

Be liberal in what you accept,
and conservative in what you send
~ Jon Postel, RFC1122

Re: hashCode/equals for entities

Posted by Christiaan <ch...@hotmail.com>.
Hi,
be carefull wit putting datastore ids in the hashCode(), since the id may
not be present when the object is created (it has to be made persistent and
flushed to the datastore). This may cause problems when you put objects in a
hashmap and later on make them persistent.

kind regards,
Christiaan
-- 
View this message in context: http://www.nabble.com/hashCode-equals-for-entities-tf4665102.html#a13344579
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: hashCode/equals for entities

Posted by Patrick Linskey <pl...@gmail.com>.
Hi,

OpenJPA never uses your entity instance's equals or hashCode methods,
so it should not matter from an impl standpoint.

>From an application-semantics standpoint, it probably makes sense to
use ID fields (and no other fields) in your equality algorithm.

-Patrick

On 10/20/07, Gavin Bong <ga...@gmail.com> wrote:
> Hi,
>
> When implementing the hashCode and equals for an entity, do
> I need to include my generated id in the calculation ?
>
> e.g
>
> public class EventModel
>     implements Serializable
> {
>
>     @Id
>     @Column( name = "id" )
>     @GeneratedValue
>     private String id;
>
>     ... snip
> }
>
> The other non-null members are already in hashCode & equals.
> Do I include the "id" above as well ?
>
> Thanks.
>
> --
> Regards,
>
> Gavin Bong (Jayway Malaysia)
> blogging at http://raverun.com/jayway
>
> -----
> Ce qui compte dans une vie,
> c'est l'intensité d'une vie,
> ce n'est pas la durée d'une vie
> ~ Jacque Brel
>
> Be liberal in what you accept,
> and conservative in what you send
> ~ Jon Postel, RFC1122
>


-- 
Patrick Linskey
202 669 5907