db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From olegn...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/accesslayer ReferencePrefetcher.java
Date Sat, 20 Sep 2003 18:39:34 GMT
olegnitz    2003/09/20 11:39:34

  Modified:    src/java/org/apache/ojb/broker/accesslayer
                        ReferencePrefetcher.java
  Log:
  The fix ispired by Oliver Matz, in case of null complex foreign keys
  
  Revision  Changes    Path
  1.10      +63 -50    db-ojb/src/java/org/apache/ojb/broker/accesslayer/ReferencePrefetcher.java
  
  Index: ReferencePrefetcher.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/ReferencePrefetcher.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- ReferencePrefetcher.java	18 Sep 2003 20:08:32 -0000	1.9
  +++ ReferencePrefetcher.java	20 Sep 2003 18:39:33 -0000	1.10
  @@ -79,42 +79,54 @@
   public class ReferencePrefetcher extends RelationshipPrefetcherImpl
   {
   
  -	/**
  -	 * Constructor for ReferencePrefetcher.
  -	 * @param aBroker
  -	 * @param anOrd
  -	 */
  -	public ReferencePrefetcher(PersistenceBroker aBroker, ObjectReferenceDescriptor anOrd)
  -	{
  -		super(aBroker, anOrd);
  -	}
  -
  -	/**
  -	 * Associate the batched Children with their owner object.
  -	 * Loop over owners
  -	 */
  -	protected void associateBatched(Collection owners, Collection children)
  -	{
  +    /**
  +    * Constructor for ReferencePrefetcher.
  +    * @param aBroker
  +    * @param anOrd
  +    */
  +    public ReferencePrefetcher(PersistenceBroker aBroker, ObjectReferenceDescriptor anOrd)
  +    {
  +        super(aBroker, anOrd);
  +    }
  +
  +    /**
  +     * Associate the batched Children with their owner object.
  +     * Loop over owners
  +     */
  +    protected void associateBatched(Collection owners, Collection children)
  +    {
           ObjectReferenceDescriptor ord = getObjectReferenceDescriptor();
  -		ClassDescriptor cld = getOwnerClassDescriptor();
  -		Object owner, relatedObject;
  -		Object fkValues[];
  -		Identity id;
  +        ClassDescriptor cld = getOwnerClassDescriptor();
  +        Object owner, relatedObject;
  +        Object fkValues[];
  +        Identity id;
           PersistenceBroker pb = getBroker();
  -		PersistentField field = ord.getPersistentField();
  +        PersistentField field = ord.getPersistentField();
           Class topLevelClass = pb.getTopLevelClass(ord.getItemClass());
           Iterator iter = owners.iterator();
   
  -		while (iter.hasNext())
  -		{
  -			owner = iter.next();
  -			fkValues = ord.getForeignKeyValues(owner,cld);
  +        while (iter.hasNext())
  +        {
  +            owner = iter.next();
  +            fkValues = ord.getForeignKeyValues(owner,cld);
               id = new Identity(null, topLevelClass, fkValues);
  -			relatedObject = pb.getObjectByIdentity(id);
  -			field.set(owner, relatedObject);
  -		}
  -	}
  +            relatedObject = pb.getObjectByIdentity(id);
  +            field.set(owner, relatedObject);
  +        }
  +    }
   
  +    private boolean isNull(Object[] arr)
  +    {
  +        for (int i = 0; i < arr.length; i++)
  +        {
  +            if (arr[i] != null)
  +            {
  +                return false;
  +            }
  +        }
  +        return true;
  +    }
  +    
       /**
        * Build the multiple queries for one relationship because of limitation of IN(...)
        * @param owners Collection containing all objects of the ONE side
  @@ -137,6 +149,10 @@
           {
               owner = iter.next();
               fkValues = ord.getForeignKeyValues(owner,cld);
  +            if (isNull(fkValues))
  +            {
  +                continue;
  +            }
               id = new Identity(null, topLevelClass, fkValues);
               if (cache.lookup(id) == null)
               {
  @@ -166,11 +182,11 @@
       }
   
       /**
  -	 * @see org.apache.ojb.broker.accesslayer.RelationshipPrefetcherImpl#buildPrefetchQueries(Collection)
  -	 */
  -	protected Query buildPrefetchQuery(Collection ids)
  -	{
  -		ObjectReferenceDescriptor ord = getObjectReferenceDescriptor();
  +     * @see org.apache.ojb.broker.accesslayer.RelationshipPrefetcherImpl#buildPrefetchQueries(Collection)
  +     */
  +    protected Query buildPrefetchQuery(Collection ids)
  +    {
  +        ObjectReferenceDescriptor ord = getObjectReferenceDescriptor();
           FieldDescriptor pkFields[] = getItemClassDescriptor().getPkFields();
           Criteria crit;
   
  @@ -183,9 +199,9 @@
               crit = buildPrefetchCriteriaMultipleKeys(ids, pkFields);
           }
   
  -		return (crit.isEmpty() ? null
  -                               : QueryFactory.newQuery(ord.getItemClass(), crit));
  -	}
  +        return (crit.isEmpty() ? null
  +                            : QueryFactory.newQuery(ord.getItemClass(), crit));
  +    }
   
       /**
        * Build the Criteria using IN(...) for single keys
  @@ -205,10 +221,7 @@
           {
               id = (Identity) iter.next();
               fkVal = id.getPrimaryKeyValues()[0];
  -            if (fkVal != null)
  -            {
  -                fkValues.add(fkVal);
  -            }
  +            fkValues.add(fkVal);
           }
   
           // create IN (...) for the single key field
  @@ -238,18 +251,18 @@
               Criteria c = new Criteria();
               id = (Identity) iter.next();
               fkVal = id.getPrimaryKeyValues();
  -            for (int i=0;i < fkVal.length;i++)
  +            for (int i=0; i < fkVal.length; i++)
               {
  -                if (fkVal[i] != null)
  +                if (fkVal[i] == null)
                   {
  -                    c.addEqualTo(pkFields[i].getAttributeName(),fkVal[i]);
  +                    c.addIsNull(pkFields[i].getAttributeName());
  +                }
  +                else
  +                {
  +                    c.addEqualTo(pkFields[i].getAttributeName(), fkVal[i]);
                   }
               }
  -
  -            if (!c.isEmpty())
  -            {
  -                crit.addOrCriteria(c);
  -            }
  +            crit.addOrCriteria(c);
           }
   
           return crit;
  
  
  

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