openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rick Curtis <curti...@gmail.com>
Subject Re: Insert new record + update child object
Date Mon, 08 Apr 2013 17:39:32 GMT
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 <maxtorzito@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*

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message