You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by "climber.chuck" <cl...@gmx.de> on 2004/03/22 19:31:11 UTC

Problem with equals() and Identity

Hi,
I hope I don't ask something which has been answered several times.
I am using the PersistenceBroker API (RC1.4) for some time and noticed a
problem if I want to implement equals() and hashCode() in my business
classes. For example:
 
Class HerstellerImpl {
String bezeichnung;
..
public boolean equals(Object o) {
            if (this == o) {
                  return true;
            }
            if (o == null) {
                  return false;
            }
            if (o.getClass() != this.getClass()) {
                  return false;
            }
            HerstellerImpl h = (HerstellerImpl) o;
 
            if (!this.getBezeichnung().equals(h.getBezeichnung())) {
                  return false;
            }
            return true;
}
// hashCode() in the same manner
}
 
Mapping Definition:
<!-- Definitions for de.jas.kassart.model.HerstellerImpl -->
   <class-descriptor
              class="de.jas.kassart.model.HerstellerImpl"
              table="HERSTELLER"
   >
      <field-descriptor
        name="_id"
        column="ID"
        jdbc-type="SMALLINT"
        primarykey="true"
        autoincrement="true"
                        access="anonymous"
      />
      <field-descriptor
        name="bezeichnung"
        column="BEZEICHNUNG"
        jdbc-type="CHAR"
      />
   </class-descriptor>
 
Now if I want to store an object which has already been stored and
which's bezeichnung has changed, OJB produces an insert statement
instead of an update statement.
I am not sure but I think the reason is that Identity(object, broker,
cld) does retrieve a new database key because AbstractPersistentField
uses a HashMap for storing objects. After changing bezeichnung equals()
and hashCode() produce new entries in this Map.
Has anybody had the same problems? Have I misunderstood something? Thank
you for every help!
Andreas Marx

Re: Problem with equals() and Identity

Posted by Andy Malakov <an...@transdecisions.com>.
Problem with internal HashMap was fixed after RC5. Try upgrading to latest.

The fact that two HerstellerImpl instances representing different database rows are considered as identical may lead to more issues
whenever Map is used for caching. See if upgrading to latest will help, otherwise I would externalize this logic into special
Comparator.


----- Original Message ----- 
From: "climber.chuck" <cl...@gmx.de>
To: <oj...@db.apache.org>
Sent: Monday, March 22, 2004 1:31 PM
Subject: Problem with equals() and Identity


> Hi,
> I hope I don't ask something which has been answered several times.
> I am using the PersistenceBroker API (RC1.4) for some time and noticed a
> problem if I want to implement equals() and hashCode() in my business
> classes. For example:
>
> Class HerstellerImpl {
> String bezeichnung;
> ..
> public boolean equals(Object o) {
>             if (this == o) {
>                   return true;
>             }
>             if (o == null) {
>                   return false;
>             }
>             if (o.getClass() != this.getClass()) {
>                   return false;
>             }
>             HerstellerImpl h = (HerstellerImpl) o;
>
>             if (!this.getBezeichnung().equals(h.getBezeichnung())) {
>                   return false;
>             }
>             return true;
> }
> // hashCode() in the same manner
> }
>
> Mapping Definition:
> <!-- Definitions for de.jas.kassart.model.HerstellerImpl -->
>    <class-descriptor
>               class="de.jas.kassart.model.HerstellerImpl"
>               table="HERSTELLER"
>    >
>       <field-descriptor
>         name="_id"
>         column="ID"
>         jdbc-type="SMALLINT"
>         primarykey="true"
>         autoincrement="true"
>                         access="anonymous"
>       />
>       <field-descriptor
>         name="bezeichnung"
>         column="BEZEICHNUNG"
>         jdbc-type="CHAR"
>       />
>    </class-descriptor>
>
> Now if I want to store an object which has already been stored and
> which's bezeichnung has changed, OJB produces an insert statement
> instead of an update statement.
> I am not sure but I think the reason is that Identity(object, broker,
> cld) does retrieve a new database key because AbstractPersistentField
> uses a HashMap for storing objects. After changing bezeichnung equals()
> and hashCode() produce new entries in this Map.
> Has anybody had the same problems? Have I misunderstood something? Thank
> you for every help!
> Andreas Marx
>


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org