You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Bruno René Santos <br...@holos.pt> on 2011/10/13 12:53:24 UTC

equal(Obj) and hashCode()

Hello all,

I've come accross a problem with the CayenneDataObjects. For some reason
Cayenne created two different instances of the same database object. This
made that the function equals and hashCode returned different values for the
same data object (conceptually). I checked the CayenneDataObject and the
hashCode function is not overriden anywhere, leading to this problem. As I
already have extended the CayenneDataObject I only had to override the
following functions:

@Override
public boolean equals(Object obj) {
if (obj instanceof HolosDataObject)
return getObjectId().equals(((HolosDataObject) obj).getObjectId());
return false;
}

@Override
public int hashCode() {
return getObjectId().hashCode();
}

Which fixed the problem. I am using cayenne 3.0.1. Shouldn't these
functions, or some sort of version of them, be present in
CayenneDataObject/PersistentDataObject?

Regards
Bruno Santos

-- 
Bruno René Santos | brunorene@holos.pt | Gestor de Projectos | Analista |
Programador | Investigador

Holos - Soluções Avançadas em Tecnologias de Informação S.A.
Parque de Ciência e Tecnologia de Almada/Setúbal . Edifício Madan Parque
Rua dos Inventores . Quinta da Torre . 2825 - 182 Caparica . Portugal
Phone: +351 210 438 686 . Fax: +351 210 438 687 . Web: www.holos.pt

This email and any files transmitted with it are confidential and intended
solely for the use of the individual or entity to whom they are addressed.
If you are not the intended recipient or the person responsible for
delivering the email to the intended recipient, be advised that you have
received this email in error and that any use, dissemination, forwarding,
printing, or copying of this email is strictly prohibited. If you have
received this email in error please notify Bruno René Santos by telephone on
+351 210 438 686

Re: equal(Obj) and hashCode()

Posted by Bruno René Santos <br...@holos.pt>.
Thanx Andrus,

Two different contexts is the problem! I was quite amazed with this as I
work for sometime with Cayenne and this problem never arose... I'll remove
the new functions.

Regards
Bruno

On Thu, Oct 13, 2011 at 12:07 PM, Andrus Adamchik <an...@objectstyle.org>wrote:

> Uniquing (a guarantee that only one copy of object with a given ID exists
> within the same ObjectContext) is Cayenne feature that works consistently
> across all releases since the early days. Please DON'T attempt to override
> equals and hashCode. The problem is likely elsewhere (likely your 2 objects
> coming from different contexts). And your implementation is not correct from
> the Cayenne POV (it doesn't take context into account) and will likely cause
> other problems.
>
> Cheers,
> Andrus
>
> On Oct 13, 2011, at 1:53 PM, Bruno René Santos wrote:
>
> > Hello all,
> >
> > I've come accross a problem with the CayenneDataObjects. For some reason
> > Cayenne created two different instances of the same database object. This
> > made that the function equals and hashCode returned different values for
> the
> > same data object (conceptually). I checked the CayenneDataObject and the
> > hashCode function is not overriden anywhere, leading to this problem. As
> I
> > already have extended the CayenneDataObject I only had to override the
> > following functions:
> >
> > @Override
> > public boolean equals(Object obj) {
> > if (obj instanceof HolosDataObject)
> > return getObjectId().equals(((HolosDataObject) obj).getObjectId());
> > return false;
> > }
> >
> > @Override
> > public int hashCode() {
> > return getObjectId().hashCode();
> > }
> >
> > Which fixed the problem. I am using cayenne 3.0.1. Shouldn't these
> > functions, or some sort of version of them, be present in
> > CayenneDataObject/PersistentDataObject?
> >
> > Regards
> > Bruno Santos
> >
> > --
> > Bruno René Santos | brunorene@holos.pt | Gestor de Projectos | Analista
> |
> > Programador | Investigador
> >
> > Holos - Soluções Avançadas em Tecnologias de Informação S.A.
> > Parque de Ciência e Tecnologia de Almada/Setúbal . Edifício Madan Parque
> > Rua dos Inventores . Quinta da Torre . 2825 - 182 Caparica . Portugal
> > Phone: +351 210 438 686 . Fax: +351 210 438 687 . Web: www.holos.pt
> >
> > This email and any files transmitted with it are confidential and
> intended
> > solely for the use of the individual or entity to whom they are
> addressed.
> > If you are not the intended recipient or the person responsible for
> > delivering the email to the intended recipient, be advised that you have
> > received this email in error and that any use, dissemination, forwarding,
> > printing, or copying of this email is strictly prohibited. If you have
> > received this email in error please notify Bruno René Santos by telephone
> on
> > +351 210 438 686
>
>
>


-- 
Bruno René Santos | brunorene@holos.pt | Gestor de Projectos | Analista |
Programador | Investigador

Holos - Soluções Avançadas em Tecnologias de Informação S.A.
Parque de Ciência e Tecnologia de Almada/Setúbal . Edifício Madan Parque
Rua dos Inventores . Quinta da Torre . 2825 - 182 Caparica . Portugal
Phone: +351 210 438 686 . Fax: +351 210 438 687 . Web: www.holos.pt

This email and any files transmitted with it are confidential and intended
solely for the use of the individual or entity to whom they are addressed.
If you are not the intended recipient or the person responsible for
delivering the email to the intended recipient, be advised that you have
received this email in error and that any use, dissemination, forwarding,
printing, or copying of this email is strictly prohibited. If you have
received this email in error please notify Bruno René Santos by telephone on
+351 210 438 686

Re: equal(Obj) and hashCode()

Posted by Andrus Adamchik <an...@objectstyle.org>.
Uniquing (a guarantee that only one copy of object with a given ID exists within the same ObjectContext) is Cayenne feature that works consistently across all releases since the early days. Please DON'T attempt to override equals and hashCode. The problem is likely elsewhere (likely your 2 objects coming from different contexts). And your implementation is not correct from the Cayenne POV (it doesn't take context into account) and will likely cause other problems.

Cheers,
Andrus

On Oct 13, 2011, at 1:53 PM, Bruno René Santos wrote:

> Hello all,
> 
> I've come accross a problem with the CayenneDataObjects. For some reason
> Cayenne created two different instances of the same database object. This
> made that the function equals and hashCode returned different values for the
> same data object (conceptually). I checked the CayenneDataObject and the
> hashCode function is not overriden anywhere, leading to this problem. As I
> already have extended the CayenneDataObject I only had to override the
> following functions:
> 
> @Override
> public boolean equals(Object obj) {
> if (obj instanceof HolosDataObject)
> return getObjectId().equals(((HolosDataObject) obj).getObjectId());
> return false;
> }
> 
> @Override
> public int hashCode() {
> return getObjectId().hashCode();
> }
> 
> Which fixed the problem. I am using cayenne 3.0.1. Shouldn't these
> functions, or some sort of version of them, be present in
> CayenneDataObject/PersistentDataObject?
> 
> Regards
> Bruno Santos
> 
> -- 
> Bruno René Santos | brunorene@holos.pt | Gestor de Projectos | Analista |
> Programador | Investigador
> 
> Holos - Soluções Avançadas em Tecnologias de Informação S.A.
> Parque de Ciência e Tecnologia de Almada/Setúbal . Edifício Madan Parque
> Rua dos Inventores . Quinta da Torre . 2825 - 182 Caparica . Portugal
> Phone: +351 210 438 686 . Fax: +351 210 438 687 . Web: www.holos.pt
> 
> This email and any files transmitted with it are confidential and intended
> solely for the use of the individual or entity to whom they are addressed.
> If you are not the intended recipient or the person responsible for
> delivering the email to the intended recipient, be advised that you have
> received this email in error and that any use, dissemination, forwarding,
> printing, or copying of this email is strictly prohibited. If you have
> received this email in error please notify Bruno René Santos by telephone on
> +351 210 438 686