db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t...@apache.org
Subject cvs commit: jakarta-ojb/src/java/org/apache/ojb/broker/singlevm PersistenceBrokerImpl.java
Date Thu, 02 Jan 2003 14:57:01 GMT
thma        2003/01/02 06:57:01

  Modified:    src/java/org/apache/ojb/broker/singlevm
                        PersistenceBrokerImpl.java
  Log:
  bugfix: objects did not get inserted if they had PK fields declared as locking!
  
  Revision  Changes    Path
  1.103     +61 -62    jakarta-ojb/src/java/org/apache/ojb/broker/singlevm/PersistenceBrokerImpl.java
  
  Index: PersistenceBrokerImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ojb/src/java/org/apache/ojb/broker/singlevm/PersistenceBrokerImpl.java,v
  retrieving revision 1.102
  retrieving revision 1.103
  diff -u -r1.102 -r1.103
  --- PersistenceBrokerImpl.java	2 Jan 2003 09:36:18 -0000	1.102
  +++ PersistenceBrokerImpl.java	2 Jan 2003 14:57:01 -0000	1.103
  @@ -1935,10 +1935,10 @@
       private void storeToDb(Object obj, ClassDescriptor cld, Identity oid, Map markedForStore,
boolean insert)
       {
           /**
  -         * only do the update/insert if the primary key is autoincrement, or is already
a valid
  -         * value.
  +         * only do the update/insert if all PK values are either defined as
  +         * autoincrement or locking, or do hold valid values.
            */
  -        if (isPKautoIncrement(cld.getPkFields()) || isPKvalid(oid.getPrimaryKeyValues()))
  +        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?)
  @@ -2007,6 +2007,63 @@
       }
   
       /**
  +     * returns true if the primary key fields are valid, else false.
  +     * PK fields are valid if each of them is either an OJB managed
  +     * attribute (autoincrement or locking) or if it contains
  +     * a valid non-null value
  +     * @param fieldDescriptors the array of PK fielddescriptors
  +     * @param pkValues the array of PK values
  +     * @return boolean
  +     */
  +    private boolean assertValidPkFields(FieldDescriptor[] fieldDescriptors, Object[] pkValues)
  +    {
  +        int fieldDescriptorSize = fieldDescriptors.length;
  +        for (int i = 0; i < fieldDescriptorSize; i++)
  +        {
  +            /**
  +             * a pk field is valid if it is either managed by OJB
  +             * (autoincrement or locking) or if it does contain a
  +             * valid non-null value.
  +             */
  +            if (! (fieldDescriptors[i].isAutoIncrement() 
  +            		|| fieldDescriptors[i].isLocking() 
  +            		|| assertValidPkValue(pkValues[i]) ))
  +            {            	
  +                return false;
  +            }
  +        }
  +        return true;
  +    }
  +
  +	/**
  +	 * returns true if a value is non-null, STring instances are also checked,
  +	 * if they are non-empty.
  +	 * @param pkValue the value to check
  +	 * @return boolean
  +	 */
  +	private boolean assertValidPkValue(Object pkValue)
  +	{
  +        // null as value of a primary key is not acceptable
  +        if (pkValue == null)
  +        {
  +            return false;
  +        }
  +        if (pkValue instanceof String)
  +        {
  +            // the toString() method on a String-object is maybe faster
  +            // than the downcast to String. Also use length() to test
  +            // if a String empty or not, this is faster than the comparing
  +            // a String-object with an empty string using the equals()-method.
  +            if (pkValue.toString().trim().length() == 0)
  +            {
  +                return false;
  +            }
  +        }	        
  +        return true;	
  +	}
  +
  +
  +    /**
        * this method will go up the inheritance heirarchy to store all the super classes
        *
        *  <p>
  @@ -2464,64 +2521,6 @@
       public boolean isRunningInServerMode()
       {
           return runningInServerMode;
  -    }
  -
  -    /**
  -     * return true if all of the FieldDescriptors is autoIncrement.
  -     * also return true if NO FieldDescriptors available
  -     * @param fdesc the primary key FieldDescriptor[]
  -     */
  -    private boolean isPKautoIncrement(FieldDescriptor[] fdesc)
  -    {
  -        int fieldDescriptorSize = fdesc.length;
  -        for (int i = 0; i < fieldDescriptorSize; i++)
  -        {
  -            /**
  -             * if one of the keys in the primary key FieldDescriptor array
  -             * is not autoincrement we return that this isn't an autoincrement PK.
  -             * We check all fields, in the case of a composite key.
  -             */
  -            if (!fdesc[i].isAutoIncrement())
  -            {
  -                return false;
  -            }
  -        }
  -        return true;
  -    }
  -
  -    /**
  -     * only checks string type keys for validity, since the default of other types of keys
is
  -     * unknown (ie int)
  -     * @param pkValues
  -     * @return true if valid
  -     */
  -    private boolean isPKvalid(Object[] pkValues)
  -    {
  -        int size = pkValues.length;
  -
  -        for (int i = 0; i < size; i++)
  -        {
  -            Object pkValue = pkValues[i];
  -            // null as value of a primary key is not acceptable
  -            if (pkValue == null)
  -            {
  -                return false;
  -            }
  -
  -            if (pkValue instanceof String)
  -            {
  -                // the toString() method on a String-object is maybe faster
  -                // than the downcast to String. Also use length() to test
  -                // if a String empty or not, this is faster than the comparing
  -                // a String-object with an empty string using the equals()-method.
  -                if (pkValue.toString().trim().length() == 0)
  -                {
  -                    return false;
  -                }
  -            }
  -        }
  -
  -        return true;
       }
   
       /*
  
  
  

Mime
View raw message