You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by José Luis Cetina <ma...@gmail.com> on 2013/04/08 18:44:48 UTC
Insert new record + update child object
Hi. I have a problem with insert and updating a reference in the same
entity.
Im trying to insert a new object (Exam) that has a reference to another
object (Person) and at the same time i want to update an attribute
(birthDate) of the Person object. The update never happens although i
set CascadeType to ALL. The only way this works is doing a persist and
after that a merge operation. Is this normal? Do i have to change
something??
I dont like the idea of a "manual update" using merge in the Person object
because i don't know how many objects (child object of Exam) the user want
to update.
Entities:
public class Exam{
@ManyToOne(cascade= CascadeType.ALL)
@JoinColumn(name = "person_id")
public Person person;
......
}
public class Person{
private Date birthDate;
@OneToMany(mappedBy = "person")
private List<Exam> exams
.......
}
public class SomeClass{
public void someMethod(){
exam = new Exam()
person.setBirthDate(new Date());
exam.setPerson(person);
someEJB.saveExam(exam);
}
}
public class someEJB(){
public void saveExam(Exam exam){
ejbContext.getUserTransaction().begin();
em.persist(exam);
//THIS WORKS
em.merge(exam.getPerson());
ejbContext.getUserTransaction().commit();
}
}
Do i have to use the MERGE method for every child object?
Here is the same question from other user:
http://stackoverflow.com/questions/11029260/jpa-with-jta-persist-entity-and-merge-cascaded-child-entities
Re: Insert new record + update child object
Posted by José Luis Cetina <ma...@gmail.com>.
Yes it works. Thanks.
El 25/04/2013 13:49, "Pinaki Poddar" <pp...@apache.org> escribió:
> public void saveExam(Exam exam){
> ejbContext.getUserTransaction().begin();
>
> //DOES THIS WORK?
> exam = em.merge(exam);
>
> ejbContext.getUserTransaction().commit();
> }
>
>
>
> -----
> Pinaki Poddar
> Chair, Apache OpenJPA Project
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Insert-new-record-update-child-object-tp7583389p7583780.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>
Re: Insert new record + update child object
Posted by Pinaki Poddar <pp...@apache.org>.
public void saveExam(Exam exam){
ejbContext.getUserTransaction().begin();
//DOES THIS WORK?
exam = em.merge(exam);
ejbContext.getUserTransaction().commit();
}
-----
Pinaki Poddar
Chair, Apache OpenJPA Project
--
View this message in context: http://openjpa.208410.n2.nabble.com/Insert-new-record-update-child-object-tp7583389p7583780.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Insert new record + update child object
Posted by Rick Curtis <cu...@gmail.com>.
It looks like your problem is that the Exam is new, yet the Person is
existing and the existing Entity gets ignored when cascading the persist
operation. I believe this is working as expected.
As long as your relationships are set to CascadeType.ALL, you could always
change your em.persist(exam); to em.merge(exam);. That would take care of
persisting the new exam, and it would also cascade the merge call to the
person.
Thanks,
Rick
On Mon, Apr 8, 2013 at 11:44 AM, José Luis Cetina <ma...@gmail.com>wrote:
> Hi. I have a problem with insert and updating a reference in the same
> entity.
>
> Im trying to insert a new object (Exam) that has a reference to another
> object (Person) and at the same time i want to update an attribute
> (birthDate) of the Person object. The update never happens although i
> set CascadeType to ALL. The only way this works is doing a persist and
> after that a merge operation. Is this normal? Do i have to change
> something??
>
> I dont like the idea of a "manual update" using merge in the Person object
> because i don't know how many objects (child object of Exam) the user want
> to update.
>
> Entities:
>
> public class Exam{
> @ManyToOne(cascade= CascadeType.ALL)
> @JoinColumn(name = "person_id")
> public Person person;
> ......
> }
>
> public class Person{
> private Date birthDate;
> @OneToMany(mappedBy = "person")
> private List<Exam> exams
> .......
> }
>
> public class SomeClass{
> public void someMethod(){
> exam = new Exam()
> person.setBirthDate(new Date());
> exam.setPerson(person);
> someEJB.saveExam(exam);
> }
> }
>
> public class someEJB(){
>
> public void saveExam(Exam exam){
> ejbContext.getUserTransaction().begin();
> em.persist(exam);
> //THIS WORKS
> em.merge(exam.getPerson());
> ejbContext.getUserTransaction().commit();
> }
>
> }
>
> Do i have to use the MERGE method for every child object?
>
>
> Here is the same question from other user:
>
> http://stackoverflow.com/questions/11029260/jpa-with-jta-persist-entity-and-merge-cascaded-child-entities
>
--
*Rick Curtis*