RE: Duplicates in to-many relationships

From: Kevin Menard (kmenar..ervprise.com)
Date: Wed Jun 20 2007 - 08:13:32 EDT

  • Next message: yongbl: "Re: Common attributes in the model"

     

    > -----Original Message-----
    > From: Alexander Lamb (dev) [mailto:alam..ac.com]
    > Sent: Wednesday, June 20, 2007 3:24 AM
    > To: use..ayenne.apache.org
    > Subject: Re: Duplicates in to-many relationships
    >
    > Well, although it works, I still have a small problem.
    >
    > When you do a "setAnObject" even if it does "magically"
    > handle the "addTo", it does so without calling the public
    > "addToMyList" function.
    >
    > This means I can't add some logic to the addTo function (I
    > need to perform some calculation each time a new object is
    > added to the list.
    >
    > So either I find some sort of "objectWasAddedToRelationship"
    > delegate message, or I will need to trigger the calculation
    > also from the "setAnObject" side.

    I think you'll find that overriding addToManyTarget() will take care of
    most (all?) cases for you, rather than override the public methods on
    each of your individual CDOs. For example, I have:

    public class EnhancedCayenneDataObject extends CayenneDataObject
    {
       ..verride
        public void addToManyTarget(final String relName, final DataObject
    value, final boolean setReverse)
        {
            final List list = (List) readProperty(relName);
            
            if (false == list.contains(value))
            {
                super.addToManyTarget(relName, value, setReverse);
            }
        }
    }

    > To answer directly your question, I do need sometimes a
    > sorted list but I usually don't touch the to-many relation. I
    > simply create a new function which sorts the to-many list. In
    > order to boost performance, I do keep an ArrayList variable
    > which is calculated once and only reset when the to-many list
    > changes. Another reason to need to be able to access the
    > addToMyList when I can reset my calculated sorted list.

    I wasn't talking about sorted, but rather ordered. List provides
    ordered semantics, while Set does not. Set, however, does not allow
    duplicates, while List does. Both ordered and unique are desired
    properties. The DB will not allow duplicates and will provide an
    ordering, so it'd be helpful if the data model matched that, IMO.

    -- 
    Kevin
    



    This archive was generated by hypermail 2.0.0 : Wed Jun 20 2007 - 08:13:57 EDT