Return-Path: X-Original-To: apmail-openjpa-users-archive@minotaur.apache.org Delivered-To: apmail-openjpa-users-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id F3F1DFAFD for ; Mon, 8 Apr 2013 17:39:57 +0000 (UTC) Received: (qmail 63424 invoked by uid 500); 8 Apr 2013 17:39:57 -0000 Delivered-To: apmail-openjpa-users-archive@openjpa.apache.org Received: (qmail 63375 invoked by uid 500); 8 Apr 2013 17:39:57 -0000 Mailing-List: contact users-help@openjpa.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@openjpa.apache.org Delivered-To: mailing list users@openjpa.apache.org Received: (qmail 63367 invoked by uid 99); 8 Apr 2013 17:39:57 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 08 Apr 2013 17:39:57 +0000 X-ASF-Spam-Status: No, hits=1.7 required=5.0 tests=FREEMAIL_ENVFROM_END_DIGIT,HTML_MESSAGE,RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of curtisr7@gmail.com designates 209.85.214.174 as permitted sender) Received: from [209.85.214.174] (HELO mail-ob0-f174.google.com) (209.85.214.174) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 08 Apr 2013 17:39:53 +0000 Received: by mail-ob0-f174.google.com with SMTP id wm15so3432891obc.5 for ; Mon, 08 Apr 2013 10:39:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=3vrVddy4jkkjO6fGKe2tRG5ezIgOxJnRgmX2RPvy3X0=; b=emnFj41vphX9ybCUt19FumNZ5unEf1g6UeOj9o/A7QD9Xq03+5ixLKG01ixPF2Qblf JObqJwcXoN5oHFVwuUmIyTfbALQy6WQUTSxaHMw5fQ1dzx8OrFfIY15W14pEu3XM6t0x iiAN7yW+UMeO8JeIftDnnrcesA6SFPGeaGEu55S9X9G1nDljN4XVL76CD0TsuaC1Eh0X tqQZvLnGeUgFhqh2EaATth2vFXCsMOQ2ufaDi4CBXa8YV+yUjZhHvLQSYLgr9ejXRbVE TF4r9tKFVF1sKMTc1A+4Ubft3T0J8EkmaXzc1afQ8Pdsb88vyXjB9W+t4jasHM4AN2J9 0wCA== MIME-Version: 1.0 X-Received: by 10.60.131.237 with SMTP id op13mr16657004oeb.60.1365442772753; Mon, 08 Apr 2013 10:39:32 -0700 (PDT) Received: by 10.60.41.34 with HTTP; Mon, 8 Apr 2013 10:39:32 -0700 (PDT) In-Reply-To: References: Date: Mon, 8 Apr 2013 12:39:32 -0500 Message-ID: Subject: Re: Insert new record + update child object From: Rick Curtis To: users Content-Type: multipart/alternative; boundary=047d7b41cdaa6e651304d9dcebcb X-Virus-Checked: Checked by ClamAV on apache.org --047d7b41cdaa6e651304d9dcebcb Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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=E9 Luis Cetina w= rote: > 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 objec= t > because i don't know how many objects (child object of Exam) the user wan= t > to update. > > Entities: > > public class Exam{ > @ManyToOne(cascade=3D CascadeType.ALL) > @JoinColumn(name =3D "person_id") > public Person person; > ...... > } > > public class Person{ > private Date birthDate; > @OneToMany(mappedBy =3D "person") > private List exams > ....... > } > > public class SomeClass{ > public void someMethod(){ > exam =3D 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-a= nd-merge-cascaded-child-entities > --=20 *Rick Curtis* --047d7b41cdaa6e651304d9dcebcb--