Re: Cayenne 101

From: Pierre Lavignotte (pierre.lavignott..mail.com)
Date: Fri Dec 26 2008 - 17:05:33 EST

  • Next message: John Armstrong: "pre-persist not firing"

    Cayenne handles reverse relationships.
    But unreferencing an object doesn't mean deleting it in a DB point of view.

    E1.getE2List().remove(e2);

    I didn't notice that, but remove the object from the list is NOT breaking
    the relation.
    You should use E1.removeFromE2Array(E2).

    Do you use the Modeler ? It is really helpfull to define you graph.

    (Just see the mail from Robert Zeigler, seems that I am out of date with the
    3.0 version...)

    Cordialement,
    Pierre Lavignotte
    Ingénieur Conception & Développement
    http://pierre.lavignotte.googlepages.com

    On Fri, Dec 26, 2008 at 9:05 PM, Joe Baldwin <jfbaldwi..arthlink.net>wrote:

    > This accounts for the totally weird results. Ref to "what [I] want". As
    > usual with software, I would like to make a change in one place vs two or
    > three places. I guess I am still thinking of this from a RDB point of view.
    > It sounds like you are telling me, that I must update all of the
    > relationships via explicit code references and then commitChanges.
    >
    > Is this correct?
    >
    >
    >
    >
    > On Dec 26, 2008, at 2:19 PM, Pierre Lavignotte wrote:
    >
    > Hi Joe,
    >>
    >> Question 1 :
    >>
    >> you can't call the object constructor. Use the DataContext method instead
    >> because it will register the object too.
    >>
    >> Question 2 :
    >> It depends what you want !
    >>
    >> DataContext dc = DataContext.
    >>
    >> getThreadDataContext();
    >>
    >>> E1.getE2List().remove(e2);
    >>> dc.commitChanges();
    >>>
    >>
    >> This will remove the link between E1 and E2 but E2 will still exist.
    >>
    >> DataContext dc = DataContext.
    >>
    >> getThreadDataContext();
    >>
    >>> dc.deleteObject(e2);
    >>> dc.commitChangesToParent();
    >>> dc.commitChanges();
    >>>
    >>
    >>
    >> This will delete E2... but I think it will still be present in
    >> E1.getE2List() because the list can be in memory.
    >> I know it's not logical but I'm quite sure I allready encontered this
    >> case.
    >>
    >> I use Cayenne 2.0.4 so Cayenne 3.0 can differ in some points.
    >>
    >> Pierre
    >>
    >>
    >> Cordialement,
    >> Pierre Lavignotte
    >> Ingénieur Conception & Développement
    >> http://pierre.lavignotte.googlepages.com
    >>
    >>
    >> On Fri, Dec 26, 2008 at 6:32 PM, Joe Baldwin <jfbaldwi..arthlink.net
    >> >wrote:
    >>
    >> I have a few questions about best practices. I have done some
    >>> experimenting, have read what I could find, and have some questions about
    >>> some elementary Cayenne usage concerning Add & Delete with a
    >>> Parent-ChildList design. (I have recently experienced some odd behavior
    >>> that may be due to a fundamental misunderstanding of DataContext rules.)
    >>>
    >>> Environment: I am using 3.0 M4 with MySQL and Tomcat JSP
    >>> DataContext: I am getting the context using:
    >>> DataContext.getThreadDataContext();
    >>> Example Design:
    >>> E1 has a list of E2's
    >>> that is:
    >>> E2 is a child of E1 and is a many-to-one relationship.
    >>> The E1 relationship is called "E2List" and the reverse relationship
    >>> on E2 is called "E1".
    >>>
    >>> Questions:
    >>> 1. When creating and associating E2 children is it more proper to do the
    >>> following:
    >>>
    >>> DataContext dc = DataContext.getThreadDataContext();
    >>> ** [get E1 via a DataContext query]
    >>> E2 e2 = new E2();
    >>> e2.setE1(e1); // I **assume** this registers e2 with the Context &
    >>> adds e2 to e1's list
    >>> dc.commitChanges();
    >>>
    >>> OR do you need to do as your example suggests:
    >>>
    >>> DataContext dc = DataContext.getThreadDataContext();
    >>> E2 e2 = (E2) dc.newObject(E2.class);
    >>> e2.setE1(e1);
    >>> dc.commitChanges();
    >>>
    >>> OR is there a better way?
    >>>
    >>> 2. Removal of a Child
    >>> Can you remove the child using:
    >>> DataContext dc = DataContext.getThreadDataContext();
    >>> dc.deleteObject(e2);
    >>> dc.commitChangesToParent();
    >>> dc.commitChanges();
    >>>
    >>> or must you do it via the parent:
    >>> DataContext dc = DataContext.getThreadDataContext();
    >>> E1.getE2List().remove(e2);
    >>> dc.commitChanges();
    >>>
    >>> (Note: the second method seems to work better.)
    >>>
    >>> Thanks,
    >>> Joe
    >>>
    >>>
    >>>
    >



    This archive was generated by hypermail 2.0.0 : Fri Dec 26 2008 - 17:06:07 EST