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

Reply via email to