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 QueryReferenceBroker.java
Date Thu, 22 Apr 2004 16:16:09 GMT
arminw      2004/04/22 09:16:09

  Modified:    src/java/org/apache/ojb/broker/core
                        QueryReferenceBroker.java
  Log:
  fix problem with 1:1 references using anonymous keys.
  
  If the object was copied all anonymous FK information is lost,
  because AnonymousPersistentField use object identity as key
  for FK values. Thus if you call PB.retrieveAllReferences on a
  copied object all 1:1 references are missed.
  
  Revision  Changes    Path
  1.14      +62 -21    db-ojb/src/java/org/apache/ojb/broker/core/QueryReferenceBroker.java
  
  Index: QueryReferenceBroker.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/QueryReferenceBroker.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- QueryReferenceBroker.java	9 Apr 2004 13:22:28 -0000	1.13
  +++ QueryReferenceBroker.java	22 Apr 2004 16:16:08 -0000	1.14
  @@ -45,6 +45,7 @@
   import org.apache.ojb.broker.metadata.FieldHelper;
   import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
   import org.apache.ojb.broker.metadata.fieldaccess.PersistentField;
  +import org.apache.ojb.broker.metadata.fieldaccess.AnonymousPersistentField;
   import org.apache.ojb.broker.query.Criteria;
   import org.apache.ojb.broker.query.Query;
   import org.apache.ojb.broker.query.QueryByCriteria;
  @@ -95,7 +96,7 @@
           boolean isRetrievalTasksCreated = (batchRetrieval && (m_retrievalTasks
== null));
           int fullSize = -1;
           int size = 0;
  -        
  +
           if (isRetrievalTasksCreated)
           {
               // Maps ReferenceDescriptors to HashSets of owners
  @@ -112,13 +113,13 @@
               // to be removed when Query.fullSize is removed
               if (iter instanceof PagingIterator)
               {
  -                fullSize = iter.fullSize();           
  +                fullSize = iter.fullSize();
               }
   
               while (iter.hasNext())
               {
                   Object candidate = iter.next();
  -                 
  +
                   /**
                    * MBAIRD
                    * candidate CAN be null in the case of materializing from an iterator
based
  @@ -133,7 +134,7 @@
                               || itemClass.isAssignableFrom(candidate.getClass()))
                       {
                           result.ojbAdd(candidate);
  -                        
  +
                           // BRJ: count added objects
                           // to be removed when Query.fullSize is removed
                           size++;
  @@ -156,7 +157,7 @@
                   }
               }
   
  -            
  +
               if (isRetrievalTasksCreated)
               {
                   // turn off auto prefetching for related proxies
  @@ -187,7 +188,7 @@
                   m_retrievalTasks = null;
               }
           }
  -        
  +
           // BRJ: store fullSize in Query to re-enable deprecated functionality
           // to be removed when Query.fullSize is removed
           if (fullSize < 0)
  @@ -195,7 +196,7 @@
               fullSize = size;	// use size of result
           }
           query.fullSize(fullSize);
  -        
  +
           return result;
       }
   
  @@ -391,7 +392,7 @@
           classToPrefetch = saveClassToPrefetch;
       }
   
  -    /**
  +   /**
        * retrieves an Object reference's Identity.
        * <br>
        * Null is returned if all foreign keys are null
  @@ -399,27 +400,67 @@
       private Identity getReferencedObjectIdentity(Object obj, ObjectReferenceDescriptor
rds, ClassDescriptor cld)
       {
           Object[] fkValues = rds.getForeignKeyValues(obj, cld);
  -        boolean allFkValuesNull = true;
  +        FieldDescriptor[] fkFieldDescriptors = rds.getForeignKeyFieldDescriptors(cld);
  +        boolean hasNullifiedFKValue = hasNullifiedFK(fkFieldDescriptors, fkValues);
  +        /*
  +        BRJ: if all fk values are null there's no referenced object
  +
  +        arminw: expect the given object has nullified FK values but the referenced
  +        object still exists. This could happend after serialization of the main object.
In
  +        this case all anonymous field (AK) information is lost - AnonymousPersistentField
class
  +        use the object identity to cache the AK values
  +        */
  +        if (hasNullifiedFKValue)
  +        {
  +            if(isAnonymousKeyReference(cld, rds))
  +            {
  +                Object referencedObject = rds.getPersistentField().get(obj);
  +                if(referencedObject != null)
  +                {
  +                    return new Identity(referencedObject, pb);
  +                }
  +            }
  +            else
  +            {
  +                return null;
  +            }
  +        }
   
  -        // BRJ: check if we have non null fk values
  -        // TBD  we should also check primitives
  -        // to avoid creation of unmaterializable proxies
  +        // ensure that top-level extents are used for Identities
  +        return new Identity(rds.getItemClass(), pb.getTopLevelClass(rds.getItemClass()),
fkValues);
  +    }
  +
  +    // BRJ: check if we have non null fk values
  +    // TBD  we should also check primitives
  +    // to avoid creation of unmaterializable proxies
  +    private boolean hasNullifiedFK(FieldDescriptor[] fkFieldDescriptors, Object[] fkValues)
  +    {
  +        boolean result = true;
           for (int i = 0; i < fkValues.length; i++)
           {
  -            if (fkValues[i] != null)
  +            if (!pb.serviceBrokerHelper().representsNull(fkFieldDescriptors[i], fkValues[i]))
               {
  -                allFkValuesNull = false;
  +                result = false;
                   break;
               }
           }
  -        // BRJ: if all fk values are null there's no referenced object
  -        if (allFkValuesNull)
  +        return result;
  +    }
  +
  +    private boolean isAnonymousKeyReference(ClassDescriptor cld, ObjectReferenceDescriptor
rds)
  +    {
  +        boolean result = false;
  +        FieldDescriptor[] fkFields = rds.getForeignKeyFieldDescriptors(cld);
  +        for(int i = 0; i < fkFields.length; i++)
           {
  -            return null;
  +            FieldDescriptor fkField = fkFields[i];
  +            if(AnonymousPersistentField.class.isAssignableFrom(fkField.getPersistentField().getClass()))
  +            {
  +                result = true;
  +                break;
  +            }
           }
  -
  -        // ensure that top-level extents are used for Identities
  -        return new Identity(rds.getItemClass(), pb.getTopLevelClass(rds.getItemClass()),
fkValues);
  +        return result;
       }
   
       /**
  
  
  

---------------------------------------------------------------------
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