Return-Path: Mailing-List: contact ojb-dev-help@db.apache.org; run by ezmlm Delivered-To: mailing list ojb-dev@db.apache.org Received: (qmail 92779 invoked from network); 10 Mar 2003 02:49:34 -0000 Received: from aus-nat-0.forgent.com (HELO awacs.forgent.com) (64.132.236.120) by daedalus.apache.org with SMTP; 10 Mar 2003 02:49:34 -0000 Received: from ausexch01.forgent.com (ausexch01 [10.10.1.4]) by awacs.forgent.com (8.11.3/8.11.3) with ESMTP id h2A2ngj23248 for ; Sun, 9 Mar 2003 20:49:42 -0600 (CST) Received: by ausexch01.forgent.com with Internet Mail Service (5.5.2653.19) id ; Sun, 9 Mar 2003 20:49:42 -0600 Message-ID: <2ED0E372698BD511A967009027869ED3011FB5DF@ausexch01.forgent.com> From: David Marquard To: "'OJB Developers List'" Subject: RE: cvs commit: db-ojb/src/java/org/apache/ojb/broker/util Broker Helper.java Date: Sun, 9 Mar 2003 20:49:41 -0600 MIME-Version: 1.0 X-Mailer: Internet Mail Service (5.5.2653.19) Content-Type: multipart/mixed; boundary="----_=_NextPart_000_01C2E6AF.B30771B0" X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N ------_=_NextPart_000_01C2E6AF.B30771B0 Content-Type: text/plain; charset="iso-8859-1" Thanks for making this change! I noticed a couple of things: 1) A few places in BrokerHelper were still field conversions after calling getAutoIncrementValue(), which now returns converted values. 2) getAutoIncrementValue() would raise an exception the generated value was null and the field type was non-primitive. It wouldn't throw an exception for a null value if the field type was a primitive. I've attached a patch that avoids the double conversions, and changes getAutoIncrementedValues() so it never throws an exception for a null auto generated value. Dave ------_=_NextPart_000_01C2E6AF.B30771B0 Content-Type: text/plain; name="autoincrement-patch.txt" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="autoincrement-patch.txt" Index: src/java/org/apache/ojb/broker/util/BrokerHelper.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: = /home/cvspublic/jakarta-ojb/src/java/org/apache/ojb/broker/util/BrokerHe= lper.java,v retrieving revision 1.14 diff -u -r1.14 BrokerHelper.java --- src/java/org/apache/ojb/broker/util/BrokerHelper.java 8 Mar 2003 = 19:22:56 -0000 1.14 +++ src/java/org/apache/ojb/broker/util/BrokerHelper.java 10 Mar 2003 = 02:33:26 -0000 @@ -211,14 +211,15 @@ FieldDescriptor fmd =3D pkFields[i]; PersistentField f =3D fmd.getPersistentField(); Object cv =3D f.get(objectOrProxy); - + =20 // handle autoincrement attributes if not filled if (fmd.isAutoIncrement()) { + // getAutoIncrementValue returns a value that = already + // has a field conversion run on it cv =3D getAutoIncrementValue(fmd, objectOrProxy, = cv); } - - if (convertToSql) + else if (convertToSql) { // BRJ : apply type and value mapping cv =3D fmd.getFieldConversion().javaToSql(cv); @@ -298,6 +299,8 @@ =20 =20 /** + * Get an autoincremented value that has already + * had a field conversion run on it. * @throws MetadataException if there is an erros accessing obj = field values * @deprecated */ @@ -309,73 +312,21 @@ Object result =3D cv; try { - // int - if ((f.getType() =3D=3D int.class) || (f.getType() = =3D=3D Integer.class)) - { - if ((f.get(obj) =3D=3D null) || (((Number) = f.get(obj)).intValue() =3D=3D 0)) - { - // lookup SeqMan for a value matching db column = an fieldconversion - result =3D = broker.serviceSequenceManager().getUniqueValue(fmd); - // reflect autoincrement value back into = object - f.set(obj, result); - } - } - // long - else if ((f.getType() =3D=3D long.class) || = (f.getType() =3D=3D Long.class)) - { - if ((f.get(obj) =3D=3D null) || (((Number) = f.get(obj)).longValue() =3D=3D 0)) - { - // lookup SeqMan for a value matching to the DB = column and field-conversion - result =3D = broker.serviceSequenceManager().getUniqueValue(fmd); - // reflect autoincrement value back into = object - f.set(obj, result); - } - } - // String - else if (String.class.isAssignableFrom(f.getType())) - { - if (f.get(obj) =3D=3D null) - { - // lookup SeqMan for a value matching to the DB = column and field-conversion - result =3D = broker.serviceSequenceManager().getUniqueValue(fmd); - // reflect autoincrement value back into = object - f.set(obj, result); - } - } - // Object - else if (!f.getType().isPrimitive()) + Object currentValue =3D f.get(obj); + if ((currentValue =3D=3D null) || ((cv instanceof = Number) && ((Number) currentValue).intValue() =3D=3D 0)) { - // only assign a value if attribute =3D=3D null - if (f.get(obj) =3D=3D null) - { - // lookup SeqMan for a value matching to the DB = column and field-conversion - result =3D = broker.serviceSequenceManager().getUniqueValue(fmd); - if (result !=3D null) - { - // reflect autoincrement value back into = object - f.set(obj, result); - } - else - { - throw new OJBRuntimeException( - "OJB ERROR: Dont know how to = autoincrement field " + - f.getDeclaringClass() + "#" + = f.getName()); - } - } - } - else - { - throw new OJBRuntimeException( - "OJB ERROR: Dont know how to autoincrement = field " + - f.getDeclaringClass() + "#" + = f.getName()); + // lookup SeqMan for a value matching db column an = fieldconversion + result =3D = broker.serviceSequenceManager().getUniqueValue(fmd); + // reflect autoincrement value back into object + f.set(obj, result); } return result; } catch (MetadataException e) { throw new PersistenceBrokerException( - "Error while trying to autoincrement field " + - f.getDeclaringClass() + "#" + f.getName(), e); + "Error while trying to autoincrement field " + + f.getDeclaringClass() + "#" + f.getName(), e); } catch (SequenceManagerException e) { @@ -403,15 +354,20 @@ FieldDescriptor fmd =3D nonPkFields[i]; PersistentField f =3D fmd.getPersistentField(); Object cv =3D f.get(o); - + =20 // handle autoincrement attributes if not filled if (fmd.isAutoIncrement()) { + // getAutoIncrementValue returns a value that already + // has a field conversion run on it cv =3D getAutoIncrementValue(fmd, o, cv); } - - // apply type and value conversion - cv =3D fmd.getFieldConversion().javaToSql(cv); + else=20 + { + // apply type and value conversion + cv =3D fmd.getFieldConversion().javaToSql(cv); + } + =20 result[i] =3D cv; } return result; @@ -435,11 +391,16 @@ // handle autoincrement attributes if not filled if (fmd.isAutoIncrement()) { + // getAutoIncrementValue returns a value that already + // has a field conversion run on it cv =3D getAutoIncrementValue(fmd, obj, cv); } + else + { + // apply type and value mapping + cv =3D fmd.getFieldConversion().javaToSql(cv); + } =20 - // apply type and value mapping - cv =3D fmd.getFieldConversion().javaToSql(cv); result[i] =3D cv; } return result; ------_=_NextPart_000_01C2E6AF.B30771B0--