Hi Guillaume,

Guillaume Nodet wrote:

I apologies to all of you and especially to Gopi Nandamuri
for the mail i posted a few days ago on this mailing list.
My intention was not to offend Gopi and in fact i
completely misunderstood his questions. In the future,
i will pay close attention, before replying to a mail,
to be sure to have really understood the meaning and to
be able to give something consistent as a reply.


We all make mistakes (except most managers) ;-)


As a good will and amendment gesture i want to propose the following
code of a PersistentField that i wrote to solve a problem i had
with nested fields.

Thanks for this contribution and detailed description (I understand the problem ;-)). I will add a test for this problem and adopt your fix ASAP. Thanks!


regards,
Armin

I think that the problem is not restrained to me.
The problem arise when using nested objects that can be nulled.
Actually, when ojb reads the first field of a nested object that is
null, an instance is created to continue the recursive walk through
the nested objects, even if the given value to set for the attribute
is null. So i needed to make two modifications for the process to work.

  * first i had overriden the 'set' method of the AbstractPersistentField
    class to early test if the value will not be set. The added test is
    borrowed from the PersistentFieldDirectAccessImpl but is performed
    before having created an instance of the nested object.

    public void set(Object targetObject, Object value) throws
MetadataException
    {
        if (isNestedField())
        {
                if (value != null || !getField().getType().isPrimitive())
                {
                setNestedObject(targetObject, fieldName, value);
            }
        }
        else
        {
            doSet(targetObject, value);
        }
    }


* the second modification i did is needed because the test on the 'set' method works only for primitive types. But if we store a Date for example in a nested object, it will be nevertheless created. But the test can not extend the test made earlier, because if we nested objects could have non-null default values for certain fields. So i overrided the 'setNestedObject' method to perform the test so that when realizing that the nested object is null, there is no need to create one to put a null value in the field. When the added test 'if (attrib != null || value != null)' is true the instanciation and setting of the attribute is skipped.

    protected void setNestedObject(Object obj, String fieldName, Object
value)
    {
        int index = fieldName.indexOf(PATH_TOKEN);
        if (index >= 0)
        {
            String name = fieldName.substring(0, index);
            PersistentField pField =
createInternPersistentField(ProxyHelper.getRealClass(obj), name);
            Object attrib = pField.get(ProxyHelper.getRealObject(obj));
            if (attrib != null || value != null)
            {
                  if (attrib == null)
                  {
                    .......
                    }
                    String nestedName = fieldName.substring(index +
PATH_TOKEN.length());
                    setNestedObject(attrib, nestedName, value);
                }
        }
        else
        {
            PersistentField pField =
createInternPersistentField(ProxyHelper.getRealClass(obj), fieldName);
            pField.set(ProxyHelper.getRealObject(obj), value);
        }

}

Best regards,

Guillaume Nodet





---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]






--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to