Another question : If I don't have @Dependent, when I nullify the child object in ClassA (A.b = null), its corresponding child records will be deleted, but parent will still be around. So, in order to delete the child object do I need @Dependent?
-----Original Message----- From: Onural, Gul (CAR:9S30) Sent: Wednesday, November 07, 2007 7:51 AM To: [email protected] Subject: RE: How to update contained entity with openjpa I already have cascade = CascadeType.ALL defined in the ClassA. Is @Dependent just to remove the child object without removing the parent, where CascadeType.ALL removes the child object when the parent object is removed ? public class ClassA { int idA; @OneToMany(mappedBy = "myA", cascade = CascadeType.ALL, targetEntity = classB, fetch = FetchType.EAGER) ClassB b; String name; ... } public class ClassB { int idB @OneToOne @JoinColumn(name = "idA") private ClassA myA; .... } -----Original Message----- From: Patrick Linskey [mailto:[EMAIL PROTECTED] Sent: Wednesday, November 07, 2007 4:35 AM To: [email protected] Subject: Re: How to update contained entity with openjpa In your first example, you'll end up adding a new row to the B table each time you do the new assignment -- you'll see an INSERT statement each time the code executes. In the second, you'll end up updating a single row in the B table each time -- you'll see an UPDATE statement. You could also set up the relation such that your first example would result in a DELETE for the old row and an INSERT for the new row. You would do this with the @Dependent annotation. -Patrick On Nov 6, 2007 7:34 PM, Gul Onural <[EMAIL PROTECTED]> wrote: > > Let say I have classB inside classA, both are @Entity and @OneToOne > relationships between the two. > > Let say I already persisted classA and classB and I want to update > variable b of the ClassA using following code. > > I am trying to understand if the database effect of assigning a "new" > ClassB to variable b (Code piece-I below) is any different than > getting b first than updating B's member variables (Code piece -II) ? > > // Pseudo code piece - I > startTransaction > X = findClassA > X.setb(new ClassB) > commitTransaction > > // Pseudo code piece - II > startTransaction > X = findClassA > X.getb.setName("...) > commitTransaction > > public class ClassA { > int idA; > @OneToMany(mappedBy = "myA", cascade = CascadeType.ALL, > targetEntity = classB, fetch = FetchType.EAGER) > ClassB b; > String name; > ... > } > > public class ClassB { > int idB > @OneToOne > @JoinColumn(name = "idA") > private ClassA myA; > .... > } > > > -- Patrick Linskey 202 669 5907
