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/odmg ObjectEnvelopeTable.java TransactionImpl.java
Date Sat, 30 Oct 2004 22:11:37 GMT
arminw      2004/10/30 15:11:37

  Modified:    src/java/org/apache/ojb/odmg Tag: OJB_1_0_RELEASE
                        ObjectEnvelopeTable.java TransactionImpl.java
  Log:
  move FK assignment of 1:n references. Instead of assign FK values
  when the objects were locked, now the FK will be assigned after the main object
  was passed to PB (written to DB).
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.32.2.5  +74 -4     db-ojb/src/java/org/apache/ojb/odmg/ObjectEnvelopeTable.java
  
  Index: ObjectEnvelopeTable.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/ObjectEnvelopeTable.java,v
  retrieving revision 1.32.2.4
  retrieving revision 1.32.2.5
  diff -u -r1.32.2.4 -r1.32.2.5
  --- ObjectEnvelopeTable.java	18 Sep 2004 13:05:55 -0000	1.32.2.4
  +++ ObjectEnvelopeTable.java	30 Oct 2004 22:11:37 -0000	1.32.2.5
  @@ -32,6 +32,8 @@
   import org.apache.ojb.broker.PersistenceBrokerFactory;
   import org.apache.ojb.broker.accesslayer.ConnectionManagerIF;
   import org.apache.ojb.broker.core.ValueContainer;
  +import org.apache.ojb.broker.core.proxy.CollectionProxy;
  +import org.apache.ojb.broker.core.proxy.IndirectionHandler;
   import org.apache.ojb.broker.core.proxy.ProxyHelper;
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.metadata.CollectionDescriptor;
  @@ -189,9 +191,10 @@
                   if(ProxyHelper.isMaterialized(mod.getObject()))
                   {
                       List referenceDescriptors = broker.getClassDescriptor(mod.getObject().getClass()).getObjectReferenceDescriptors();
  -                    assignReferenceFKs(broker, mod.getObject(), referenceDescriptors);
  +                    assignSingleReferenceFKs(broker, mod.getObject(), referenceDescriptors);
                   }
                   mod.getModificationState().commit(mod, broker);
  +                assignCollectionFKs(broker, broker.getClassDescriptor(mod.getObject().getClass()),
mod.getObject());
               }
           }
       }
  @@ -200,7 +203,7 @@
        * 	assigns all foreign key attributes of the Object obj.
        *  used during store(obj, boolean);
        */
  -    private void assignReferenceFKs(PersistenceBroker broker, Object obj, List referenceDescriptors)
throws PersistenceBrokerException
  +    private void assignSingleReferenceFKs(PersistenceBroker broker, Object obj, List referenceDescriptors)
throws PersistenceBrokerException
       {
           try
           {
  @@ -210,7 +213,7 @@
               {
                   ObjectReferenceDescriptor rds = (ObjectReferenceDescriptor) i.next();
                   Object ref = rds.getPersistentField().get(obj);
  -                assertFkAssignment(broker, obj, ref, rds);
  +                singleFkAssignment(broker, obj, ref, rds);
               }
           }
           catch (Throwable t)
  @@ -219,7 +222,7 @@
           }
       }
   
  -    private void assertFkAssignment(PersistenceBroker broker, Object obj, Object ref, ObjectReferenceDescriptor
rds)
  +    private void singleFkAssignment(PersistenceBroker broker, Object obj, Object ref, ObjectReferenceDescriptor
rds)
       {
           try
           {
  @@ -254,6 +257,73 @@
               throw new PersistenceBrokerException(t);
           }
       }
  +
  +    private void assignCollectionFKs(PersistenceBroker broker, ClassDescriptor cld, Object
mainObject)
  +            throws PersistenceBrokerException
  +    {
  +        Iterator i = cld.getCollectionDescriptors().iterator();
  +        while (i.hasNext())
  +        {
  +            CollectionDescriptor cds = (CollectionDescriptor) i.next();
  +            Object col = cds.getPersistentField().get(mainObject);
  +            if (col != null)
  +            {
  +                CollectionProxy proxy = ProxyHelper.getCollectionProxy(col);
  +                // if we found an unmaterialized proxy we skip FK assignment
  +                if (proxy != null && !proxy.isLoaded())
  +                {
  +                    continue;
  +                }
  +                // get PK values of the main object
  +                // BRJ: do not convertToSql
  +                ValueContainer[] objPkValues = broker.serviceBrokerHelper().getKeyValues(cld,
mainObject, false);
  +                // get foreign key info for collection elements
  +                ClassDescriptor itemCld = broker.getClassDescriptor(cds.getItemClass());
  +                FieldDescriptor[] itemFkFields = cds.getForeignKeyFieldDescriptors(itemCld);
  +
  +                // get Iterator for Collection or Array
  +                Iterator colIterator = BrokerHelper.getCollectionIterator(col);
  +                Object item = null;
  +                while (colIterator.hasNext())
  +                {
  +                    item = colIterator.next();
  +                    IndirectionHandler handler = ProxyHelper.getIndirectionHandler(item);
  +                    if (handler != null)
  +                    {
  +                        if (!handler.alreadyMaterialized())
  +                        {
  +                            continue;
  +                        }
  +                        else
  +                        {
  +                            // @todo consider registering to hear when this is
  +                            // derefernced instead of just loading here -bmc
  +                            item = handler.getRealSubject();
  +                        }
  +                    }
  +                    // assign FK in referenced objects for all 1:n references
  +                    if(!cds.isMtoNRelation())
  +                    {
  +                        //if itemCld refers to an interface the foreignKeyFieldDescriptors
  +                        //have to be computed again for each concrete class
  +                        if (itemCld.isInterface())
  +                        {
  +                            ClassDescriptor concreteItemCld = broker.getClassDescriptor(item.getClass());
  +                            itemFkFields = cds.getForeignKeyFieldDescriptors(concreteItemCld);
  +                        }
  +                        // provide all items in collection attributes
  +                        // with foreign key information
  +                        for (int j = 0; j < itemFkFields.length; j++)
  +                        {
  +                            FieldDescriptor fld = itemFkFields[j];
  +                            fld.getPersistentField().set(item, objPkValues[j].getValue());
  +                        }
  +                    }
  +                }
  +            }
  +        }
  +    }
  +
   
   	/**
   	 *
  
  
  
  1.59.2.5  +44 -86    db-ojb/src/java/org/apache/ojb/odmg/TransactionImpl.java
  
  Index: TransactionImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/TransactionImpl.java,v
  retrieving revision 1.59.2.4
  retrieving revision 1.59.2.5
  diff -u -r1.59.2.4 -r1.59.2.5
  --- TransactionImpl.java	18 Sep 2004 13:05:55 -0000	1.59.2.4
  +++ TransactionImpl.java	30 Oct 2004 22:11:37 -0000	1.59.2.5
  @@ -29,7 +29,6 @@
   import org.apache.ojb.broker.PersistenceBrokerException;
   import org.apache.ojb.broker.PersistenceBrokerFactory;
   import org.apache.ojb.broker.PersistenceBrokerSQLException;
  -import org.apache.ojb.broker.core.ValueContainer;
   import org.apache.ojb.broker.core.proxy.CollectionProxy;
   import org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl;
   import org.apache.ojb.broker.core.proxy.CollectionProxyListener;
  @@ -38,7 +37,6 @@
   import org.apache.ojb.broker.core.proxy.ProxyHelper;
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.metadata.CollectionDescriptor;
  -import org.apache.ojb.broker.metadata.FieldDescriptor;
   import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
   import org.apache.ojb.broker.util.BrokerHelper;
   import org.apache.ojb.broker.util.GUID;
  @@ -848,99 +846,59 @@
           }
           if (useImplicitLocking)
           {
  -            lockCollections(cld, objectToRegister, assLockMode);
  -        }
  -    }
  -
  -    private void lockCollections(ClassDescriptor cld, Object newTxObject, int lockMode)
  -            throws PersistenceBrokerException
  -    {
  -        Iterator i;
  -        i = cld.getCollectionDescriptors().iterator();
  -        while (i.hasNext())
  -        {
  -            CollectionDescriptor cds = (CollectionDescriptor) i.next();
  -            Object col = cds.getPersistentField().get(newTxObject);
  -            if (col != null)
  +            Iterator i = cld.getCollectionDescriptors().iterator();
  +            while (i.hasNext())
               {
  -                CollectionProxy proxy = ProxyHelper.getCollectionProxy(col);
  -                if (proxy != null)
  -                {
  -                    if (!proxy.isLoaded())
  -                    {
  -                        if (log.isDebugEnabled()) log.debug("adding self as listener to
collection proxy");
  -                        proxy.addListener(this);
  -                        continue;
  -                    }
  -                }
  -                Iterator colIterator = BrokerHelper.getCollectionIterator(col);
  -                // get foreign info for collection elements
  -                ClassDescriptor itemCld = this.getBroker().getClassDescriptor(cds.getItemClass());
  -                // BRJ: do not convertToSql
  -                ValueContainer[] objPkValues = this.getBroker().serviceBrokerHelper().getKeyValues(cld,
newTxObject, false);
  -                FieldDescriptor[] itemFkFields = cds.getForeignKeyFieldDescriptors(itemCld);
  -
  -                Object item = null;
  -                try
  +                CollectionDescriptor cds = (CollectionDescriptor) i.next();
  +                Object col = cds.getPersistentField().get(objectToRegister);
  +                if (col != null)
                   {
  -                    while (colIterator.hasNext())
  +                    CollectionProxy proxy = ProxyHelper.getCollectionProxy(col);
  +                    if (proxy != null)
                       {
  -                        item = colIterator.next();
  -                        IndirectionHandler handler = ProxyHelper.getIndirectionHandler(item);
  -                        if (handler != null)
  +                        if (!proxy.isLoaded())
                           {
  -                            if (!handler.alreadyMaterialized())
  -                            {
  -                                continue;
  -                            }
  -                            else
  -                            {
  -                                // @todo consider registering to hear when this is
  -                                // derefernced instead of just loading here -bmc
  -                                item = handler.getRealSubject();
  -                            }
  +                            if (log.isDebugEnabled()) log.debug("adding self as listener
to collection proxy");
  +                            proxy.addListener(this);
  +                            continue;
                           }
  -                        if(!cds.isMtoNRelation())
  +                    }
  +                    Iterator colIterator = BrokerHelper.getCollectionIterator(col);
  +                    Object item = null;
  +                    try
  +                    {
  +                        while (colIterator.hasNext())
                           {
  -                            //if itemCld refers to an interface the foreignKeyFieldDescriptors
  -                            //have to be computed again for each concrete class
  -                            if (itemCld.isInterface())
  -                            {
  -                                ClassDescriptor concreteItemCld = getBroker().getClassDescriptor(item.getClass());
  -                                itemFkFields = cds.getForeignKeyFieldDescriptors(concreteItemCld);
  -                            }
  -                            // provide all items in collection attributes
  -                            // with foreign key information before locking them!
  -                            for (int j = 0; j < itemFkFields.length; j++)
  +                            item = colIterator.next();
  +                            IndirectionHandler handler = ProxyHelper.getIndirectionHandler(item);
  +                            if (handler != null)
                               {
  -                                FieldDescriptor fld = itemFkFields[j];
  -                                fld.getPersistentField().set(item, objPkValues[j].getValue());
  +                                if (!handler.alreadyMaterialized())
  +                                {
  +                                    handler.addListener(this);
  +                                    continue;
  +                                }
  +                                else
  +                                {
  +                                    // @todo consider registering to hear when this is
  +                                    // derefernced instead of just loading here -bmc
  +                                    item = handler.getRealSubject();
  +                                }
                               }
  +                            doLock(item, assLockMode);
                           }
  -                        doLock(item, lockMode);
                       }
  -                }
  -                catch (PersistenceBrokerException e)
  -                {
  -                    String eol = SystemUtils.LINE_SEPARATOR;
  -                    log.error("Error while set FK in collection references[" +
  -                            eol + "current reference descriptor:" +
  -                            eol + cds.toXML() +
  -                            eol + "current item object: " + item +
  -                            eol + "main object class: " + newTxObject.getClass().getName()
+
  -                            eol + "]", e);
  -                    throw e;
  -                }
  -                catch (LockNotGrantedException e)
  -                {
  -                    String eol = SystemUtils.LINE_SEPARATOR;
  -                    log.error("Lock not granted, while set FK in collection references["
+
  -                            eol + "current reference descriptor:" +
  -                            eol + cds.toXML() +
  -                            eol + "object to lock: " + item +
  -                            eol + "main object class: " + newTxObject.getClass().getName()
+
  -                            eol + "]", e);
  -                    throw e;
  +                    catch (LockNotGrantedException e)
  +                    {
  +                        String eol = SystemUtils.LINE_SEPARATOR;
  +                        log.error("Lock not granted, while lock collection references["
+
  +                                eol + "current reference descriptor:" +
  +                                eol + cds.toXML() +
  +                                eol + "object to lock: " + item +
  +                                eol + "main object class: " + objectToRegister.getClass().getName()
+
  +                                eol + "]", e);
  +                        throw e;
  +                    }
                   }
               }
           }
  
  
  

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