cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mike Kienenberger" <>
Subject Re: Null pointer exceptions and database null
Date Thu, 03 Aug 2006 21:03:01 GMT
On 8/3/06, √ėyvind Harboe <> 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
        if (null != oldValue)
            if (oldValue.equals(value))
                // NOOP
                // IMPLEMENT: not sure if this is safe!

        super.writeProperty(propName, value);

View raw message