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

Reply via email to