db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b..@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/singlevm PersistenceBrokerImpl.java
Date Thu, 13 Mar 2003 17:22:43 GMT
brj         2003/03/13 09:22:43

  Modified:    src/java/org/apache/ojb/broker/singlevm
                        PersistenceBrokerImpl.java
  Log:
  fixed problem with references being part of the primary key
  
  Revision  Changes    Path
  1.140     +76 -65    db-ojb/src/java/org/apache/ojb/broker/singlevm/PersistenceBrokerImpl.java
  
  Index: PersistenceBrokerImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/singlevm/PersistenceBrokerImpl.java,v
  retrieving revision 1.139
  retrieving revision 1.140
  diff -u -r1.139 -r1.140
  --- PersistenceBrokerImpl.java	7 Mar 2003 17:45:00 -0000	1.139
  +++ PersistenceBrokerImpl.java	13 Mar 2003 17:22:41 -0000	1.140
  @@ -1762,78 +1762,89 @@
        * @param oid
        * @param insert
        */
  -    private void storeToDb(Object obj, ClassDescriptor cld, Identity oid, boolean insert)
  -    {
  -        /**
  -         * only do the update/insert if all PK values are either defined as
  -         * autoincrement or locking, or do hold valid values.
  -         */
  -        if (assertValidPkFields(cld.getPkFields(), oid.getPrimaryKeyValues()))
  -        {
  -            // 1. assign foreign key values so that they can be stored in step 2
  -            // todo: this should be done through sequenceManager so that it can be handled
(ignored?)
  -            // if using db auto_increment (ex: SequenceManagerMySQLImpl)
  -            assignReferenceFKs(obj, cld.getObjectReferenceDescriptors());
  -            // 2. store references (1:1) associations to avoid FK violations
  -            storeReferences(obj, cld.getObjectReferenceDescriptors());
  -
  -            // setreferenceFKs for auto_inc dbs - this can be ignored by hi low or any
that do pre assignment
  -            try
  -            {
  -
  -                sequenceManager.setReferenceFKs(obj, cld);
  -            }
  -            catch (SequenceManagerException e)
  -            {
  -                logger.error("Setting of reference FK failed", e);
  -                throw new PersistenceBrokerException(e);
  -            }
  -
  -            /**
  -             * now for multiple table inheritance
  -             * 1. store super classes, topmost parent first
  -             * 2. go down through heirarchy until current class
  -             * 3. todo: store to full extent?
  -              */
  -            storeSuperClasses(obj, cld, oid, insert);
  -
  -
  -
  -            // 3. store primitive typed attributes (Or is THIS step 3 ?)
  -            // if obj not present in db use INSERT
  -            if (insert)
  -            {
  -                dbAccess.executeInsert(cld, obj);
  -                 // Let SequenceManager update id if necessary, should only happen after
an insert
  -                try
  -                {
  -                    sequenceManager.afterStore(dbAccess, cld, obj);
  -                }
  -                catch (SequenceManagerException e)
  -                {
  -                    logger.error("SQLException during SequenceManager.afterStore (for a
" +
  -                            cld.getClassOfObject().getName() + "): " + e.getMessage(),
e);
  -                    throw new PersistenceBrokerException(e);
  -                }
  -            }
  -            // else use UPDATE
  -            else
  -            {
  -                dbAccess.executeUpdate(cld, obj);
  -            }
  -
  -
  -            // cache object for symmetry with getObjectByXXX()
  -
  -            // Create a new Identity based on the current set of primary key values.
  -            Identity newOid = new Identity(obj, this, cld);
  -            // Add the object to the cache.
  -            objectCache.cache(newOid, obj);
  -
  -            // 4. store 1:n and m:n associations
  -            storeCollections(obj, cld.getCollectionDescriptors());
  -        }
  -    }
  +	private void storeToDb(Object obj, ClassDescriptor cld, Identity oid, boolean insert)
  +	{
  +		Object[] pkValues = oid.getPrimaryKeyValues();
  +
  +		// only do the update/insert if all PK values are either defined as
  +		// autoincrement or locking, or do hold valid values.
  +        // BRJ: if the first check fails, assign foreign keys and check again
  +		if (!assertValidPkFields(cld.getPkFields(), pkValues))
  +		{
  +            // BRJ: assign fk values, they may be part of pk        
  +			// 1. assign foreign key values so that they can be stored in step 2
  +			assignReferenceFKs(obj, cld.getObjectReferenceDescriptors());
  +
  +			pkValues = serviceBrokerHelper().getKeyValues(cld, obj);
  +			if (!assertValidPkFields(cld.getPkFields(), pkValues))
  +			{
  +				throw new PersistenceBrokerException("assertValidPkFields failed !");
  +			}
  +		}
  +		else
  +		{
  +			// 1. assign foreign key values so that they can be stored in step 2
  +			// todo: this should be done through sequenceManager so that it can be handled (ignored?)
  +			// if using db auto_increment (ex: SequenceManagerMySQLImpl)
  +			assignReferenceFKs(obj, cld.getObjectReferenceDescriptors());
  +		}
  +
  +		// 2. store references (1:1) associations to avoid FK violations
  +		storeReferences(obj, cld.getObjectReferenceDescriptors());
  +
  +		// setreferenceFKs for auto_inc dbs - this can be ignored by hi low or any that do pre
assignment
  +		try
  +		{
  +			sequenceManager.setReferenceFKs(obj, cld);
  +		}
  +		catch (SequenceManagerException e)
  +		{
  +			logger.error("Setting of reference FK failed", e);
  +			throw new PersistenceBrokerException(e);
  +		}
  +
  +		/**
  +		 * now for multiple table inheritance
  +		 * 1. store super classes, topmost parent first
  +		 * 2. go down through heirarchy until current class
  +		 * 3. todo: store to full extent?
  +		  */
  +		storeSuperClasses(obj, cld, oid, insert);
  +
  +		// 3. store primitive typed attributes (Or is THIS step 3 ?)
  +		// if obj not present in db use INSERT
  +		if (insert)
  +		{
  +			dbAccess.executeInsert(cld, obj);
  +			// Let SequenceManager update id if necessary, should only happen after an insert
  +			try
  +			{
  +				sequenceManager.afterStore(dbAccess, cld, obj);
  +			}
  +			catch (SequenceManagerException e)
  +			{
  +				logger.error(
  +					"SQLException during SequenceManager.afterStore (for a " + cld.getClassOfObject().getName()
+ "): " + e.getMessage(),
  +					e);
  +				throw new PersistenceBrokerException(e);
  +			}
  +		}
  +		// else use UPDATE
  +		else
  +		{
  +			dbAccess.executeUpdate(cld, obj);
  +		}
  +
  +		// cache object for symmetry with getObjectByXXX()
  +
  +		// Create a new Identity based on the current set of primary key values.
  +		Identity newOid = new Identity(obj, this, cld);
  +		// Add the object to the cache.
  +		objectCache.cache(newOid, obj);
  +
  +		// 4. store 1:n and m:n associations
  +		storeCollections(obj, cld.getCollectionDescriptors());
  +	}
   
       /**
        * returns true if the primary key fields are valid, else false.
  
  
  

Mime
View raw message