Re: Cayenne 101

From: Pierre Lavignotte (pierre.lavignott..mail.com)
Date: Fri Dec 26 2008 - 14:19:23 EST

  • Next message: Joe Baldwin: "Re: Cayenne 101"

    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 - 14:19:55 EST