db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/core PersistenceBrokerImpl.java
Date Sun, 28 Mar 2004 01:05:33 GMT
arminw      2004/03/27 17:05:33

  Modified:    src/java/org/apache/ojb/broker/core
                        PersistenceBrokerImpl.java
  Log:
  - try to fix issue 245 by Jordi Murgo
  don't set FK field to null when the field was part
  of the PK too.
  
  Revision  Changes    Path
  1.75      +42 -21    db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerImpl.java
  
  Index: PersistenceBrokerImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerImpl.java,v
  retrieving revision 1.74
  retrieving revision 1.75
  diff -u -r1.74 -r1.75
  --- PersistenceBrokerImpl.java	26 Mar 2004 21:31:57 -0000	1.74
  +++ PersistenceBrokerImpl.java	28 Mar 2004 01:05:33 -0000	1.75
  @@ -929,12 +929,6 @@
        */
       protected void link(Object targetObject, ClassDescriptor cld, ObjectReferenceDescriptor
rds, Object referencedObject, boolean insert)
       {
  -        FieldDescriptor[] objFkFields = rds.getForeignKeyFieldDescriptors(cld);
  -        ValueContainer[] refPkValues;
  -        if (objFkFields == null)
  -        {
  -            throw new PersistenceBrokerException("No ForeignKeyFields defined in "+cld.getClassNameOfObject());
  -        }
           // MBAIRD: we have 'disassociated' this object from the referenced object,
           // the object represented by the reference descriptor is now null, so set
           // the fk in the target object to null.
  @@ -943,29 +937,19 @@
           if (referencedObject == null)
           {
               /*
  +            arminw:
               if update we set FK fields to 'null', because reference was disassociated
               We do nothing on insert, maybe only the FK fields of main object (without
               materialization of the reference object) are set by the user
               */
               if(!insert)
               {
  -                refPkValues = null;
  -                for (int i = 0; i < objFkFields.length; i++)
  -                {
  -                    objFkFields[i].getPersistentField().set(targetObject, null);
  -                }
  +                unlinkFK(targetObject, cld, rds);
               }
           }
           else
           {
  -            // BRJ: ref may be Proxy
  -            Class refClass = ProxyHelper.getRealClass(referencedObject);
  -            ClassDescriptor refCld = getClassDescriptor(refClass);
  -            refPkValues = brokerHelper.getKeyValues(refCld, referencedObject, false);
  -            for (int i = 0; i < objFkFields.length; i++)
  -            {
  -                objFkFields[i].getPersistentField().set(targetObject, refPkValues != null
? refPkValues[i].getValue(): null);
  -            }
  +            setFKField(targetObject, cld, rds, referencedObject);
           }
       }
   
  @@ -979,14 +963,51 @@
        */
       public void unlinkFK(Object targetObject, ClassDescriptor cld, ObjectReferenceDescriptor
rds)
       {
  +        setFKField(targetObject, cld, rds, null);
  +    }
  +
  +    /**
  +     * Set the FK value on the target object, extracted from the referenced object. If
the referenced object was
  +     * <i>null</i> the FK values were set to <i>null</i>, expect
when the FK field was declared as PK.
  +     *
  +     * @param targetObject real (non-proxy) target object
  +     * @param cld {@link ClassDescriptor} of the real target object
  +     * @param rds An {@link ObjectReferenceDescriptor} or {@link CollectionDescriptor}
  +     * @param referencedObject The referenced object or <i>null</i>
  +     */
  +    private void setFKField(Object targetObject, ClassDescriptor cld, ObjectReferenceDescriptor
rds, Object referencedObject)
  +    {
  +        ValueContainer[] refPkValues;
  +        FieldDescriptor fld;
           FieldDescriptor[] objFkFields = rds.getForeignKeyFieldDescriptors(cld);
           if (objFkFields == null)
           {
               throw new PersistenceBrokerException("No foreign key fields defined for class
'"+cld.getClassNameOfObject()+"'");
           }
  +        if(referencedObject == null)
  +        {
  +            refPkValues = null;
  +        }
  +        else
  +        {
  +            Class refClass = ProxyHelper.getRealClass(referencedObject);
  +            ClassDescriptor refCld = getClassDescriptor(refClass);
  +            refPkValues = brokerHelper.getKeyValues(refCld, referencedObject, false);
  +        }
           for (int i = 0; i < objFkFields.length; i++)
           {
  -            objFkFields[i].getPersistentField().set(targetObject, null);
  +            fld = objFkFields[i];
  +            /*
  +            arminw:
  +            we set the FK value when the extracted PK fields from the referenced object
are not null at all
  +            or if null, the FK field was not a PK field of target object too.
  +            Should be ok, because the values of the extracted PK field values should never
be null and never
  +            change, so it doesn't matter if the target field is a PK too.
  +            */
  +            if(refPkValues != null || !fld.isPrimaryKey())
  +            {
  +                fld.getPersistentField().set(targetObject, refPkValues != null ? refPkValues[i].getValue():
null);
  +            }
           }
       }
   
  
  
  

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


Mime
View raw message