Re: Null pointer exceptions and database null

From: Mike Kienenberger (mkienen..mail.com)
Date: Thu Aug 03 2006 - 17:03:01 EDT

  • Next message: Øyvind Harboe: "Re: Null pointer exceptions and database null"

    On 8/3/06, Øyvind Harboe <oyvind.harbo..ylin.com> wrote:
    > Is there a way to tell Cayenne to return some other value than null
    > when the database contains null?
    >
    > E.g. for String I want an empty string instead of null, for Integer I
    > want 0 instead of null, etc.
    >
    > Also, I want Cayenne not to modify the database unecessarily, e.g. if
    > Cayenne returns "" when the database contains NULL and I write back
    > "" to the database, I don't want Cayenne to write an empty string to
    > the database, but leave the database unchanged, yet there needs to be
    > a way to set such database null strings to "".

    This is an example of automatically translating empty strings to null
    when setting Cayenne DataObject values. It also makes sure not to
    reset the value if it was already null. It sounds like you'd want to
    switch empty-string and null. (This is your second concern).

    You'd just want to write a matching readProperty function to reverse
    the translation to address your first concern. Then you just put
    these methods in a base DataObject class and have all of your
    DataObjects inherit from this instead of CayenneDataObject (which is
    what BaseDataObject would inherit from).

        public void writeProperty(String propName, Object value)
        {
            // Oracle can't handle empty strings, and JSF generates them
    instead of nulls.
            if ( (value instanceof String) && (0 == ((String)value).length()) )
            {
                value = null;
            }

            // TODO: needed for JSF
            Object oldValue = readProperty(propName);
            if (oldValue == value)
            {
                // NOOP
                return;
            }
            if (null != oldValue)
            {
                if (oldValue.equals(value))
                {
                    // NOOP
                    // IMPLEMENT: not sure if this is safe!
                    return;
                }
            }

            super.writeProperty(propName, value);
        }



    This archive was generated by hypermail 2.0.0 : Thu Aug 03 2006 - 17:03:27 EDT