Re: OJB: Identity equals method impl.
Armin Waibel <[email protected]>
| Newsgroups | gmane.comp.jakarta.ojb.user |
|---|---|
| Message-ID | <[email protected]> |
Hi,
Virgo Smart wrote:
>
> In my attempt to solve a problem when retrieving object references (collection and simple) from a domain/persistent class, I looked up Identity.java class source as I suspect identity object as the source of the problem.
>
> The equals method of the identity class does not seem to take the object's real class type (m_objectsRealClass attrib) into account when comparing two identities. Is the equals method correctly implemented ?
>
yep! It's expected behavior. In certain circumstances it's not possible
to resolve the real class of an object when building the Identity. For
example: Interface Article with real classes CdArticle and BookArticle
(in mapping file Article is declared with "extent" classes CdArticle and
BookArticle). Now we define a 1:1 reference from Class A to Article. In
this case we don't know the real class but the top-level class and can
build the Identity object using the FK fields (in class A) and the
top-level class.
Long time ago it seems to be a good solution to use the top-level class
with PK fields as object Identity. This help to avoid problems with
"declared class" (interface, abstract class or real class) and the real
class of an object in references.
Could you describe more detailed (e.g. with pseudo code or a unit test +
class mapping) what your problem is?
regards,
Armin
> Equals method in Identity.java:
>
> public boolean equals(final Object obj)
> {
> if(this == obj) return true;
>
> boolean result = false;
> if (obj instanceof Identity)
> {
> final Identity id = (Identity) obj;
> result = m_objectsTopLevelClass.equals(id.m_objectsTopLevelClass) && isTransient == id.isTransient;
> if(result)
> {
> final Object[] otherPkValues = id.m_pkValues;
> result = m_pkValues.length == otherPkValues.length;
> if(result)
> {
> for (int i = 0; result && i < m_pkValues.length; i++)
> {
> result = (m_pkValues[i] == null) ? (otherPkValues[i] == null)
> : m_pkValues[i].equals(otherPkValues[i]);
>
> // special treatment for byte[]
> if (!result && m_pkValues[i] instanceof byte[] && otherPkValues[i] instanceof byte[])
> {
> result = Arrays.equals((byte[]) m_pkValues[i], (byte[]) otherPkValues[i]);
> }
> }
> }
> }
> }
> return result;
> }
>
>
> Javadoc for Identity.java class:
>
> public class Identity
> extends java.lang.Object
> implements java.io.Serializable
>
> Represents the identity of an object.
> It's composed of:
>
> class of the real object
>
> top-level class of the real object (could be an abstract class or interface or the class of the object itself), used to make an object unique across extent classes
>
> an array of all primary key value objects
>
> a flag which indicates whether this is a transient Identity (identity of a non-persistent, "new" object) or a persistent Identity (identity object of a persistent, "already written to datastore" object).
>
> Thanks and Regards,
> Gautam.
>
>
>