Return-Path: Delivered-To: apmail-openjpa-users-archive@locus.apache.org Received: (qmail 58715 invoked from network); 18 Dec 2008 19:30:45 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 18 Dec 2008 19:30:45 -0000 Received: (qmail 16147 invoked by uid 500); 18 Dec 2008 19:30:57 -0000 Delivered-To: apmail-openjpa-users-archive@openjpa.apache.org Received: (qmail 16122 invoked by uid 500); 18 Dec 2008 19:30: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 16111 invoked by uid 99); 18 Dec 2008 19:30:57 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 18 Dec 2008 11:30:57 -0800 X-ASF-Spam-Status: No, hits=2.4 required=10.0 tests=HTML_MESSAGE,SPF_PASS,WHOIS_MYPRIVREG X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: local policy) Received: from [206.190.38.187] (HELO web51408.mail.re2.yahoo.com) (206.190.38.187) by apache.org (qpsmtpd/0.29) with SMTP; Thu, 18 Dec 2008 19:30:38 +0000 Received: (qmail 53474 invoked by uid 60001); 18 Dec 2008 19:30:16 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:X-Mailer:Date:From:Reply-To:Subject:To:In-Reply-To:MIME-Version:Content-Type:Message-ID; b=BX4BkLcPEskxdQWPS+rAXa81ZB9lP9YrqALKu8IbFiEjQwpPZ2hS7h8CygM9egkbCOkDkdLKsoFAZpbUMXZzSBVo+/I6fGiwqeqZqKfHO4nu1enCHHJXz4+ZG7dkUbaKmE/NdeYCshNSemX9AB/2GSzmbl+CCi+tyBA+MLGNIuA=; X-YMail-OSG: .2wDlAIVM1ku7f7Naz4JIP187n7i9SsJ1w7FL1o33M.22Tvm589NFUcM8rcDr3RCOcOzD9bQPGUWX0RrFZn503WejvqfSzqxzB8bAZTGR7Z5JggLJKMtBO1DGl6j4lybanJ82CThTelmwnsZsaciBHP5GLMcnhNvzYOynqGj97uc7Dzeik.KMeALQkvZYRkMSNM5LCr15_2TIedId_ZKqCODCnfUggTr3hdP3Dw- Received: from [99.13.228.137] by web51408.mail.re2.yahoo.com via HTTP; Thu, 18 Dec 2008 11:30:16 PST X-Mailer: YahooMailWebService/0.7.260.1 Date: Thu, 18 Dec 2008 11:30:16 -0800 (PST) From: Sandhya Turaga Reply-To: turagasandy@yahoo.com Subject: Re: Unexpected merge/cascade behavior To: users@openjpa.apache.org In-Reply-To: <370488.95139.qm@web51408.mail.re2.yahoo.com> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="0-411714691-1229628616=:52755" Message-ID: <538255.52755.qm@web51408.mail.re2.yahoo.com> X-Virus-Checked: Checked by ClamAV on apache.org --0-411714691-1229628616=:52755 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable Please ignore my previous email on this. I have reproduced the problem and = I am trying to debug it. If I do not understand what is going on I will pos= t the testcase. Thanks Sandhya --- On Thu, 12/18/08, Sandhya Turaga wrote: From: Sandhya Turaga Subject: Re: Unexpected merge/cascade behavior To: users@openjpa.apache.org Date: Thursday, December 18, 2008, 10:13 AM Hi Jonatan, =A0=A0=A0=A0=A0 I have used your testcase to reproduce the problem but It i= s merging fine and creating both the children and parent successfully. I have used JP= A trunk to test this. Could you please run your testcase with JPA2.0 and tell= me if you are still facing the problem? Thanks Sandhya --- On Wed, 12/17/08, Jonatan Samoocha wrote: From: Jonatan Samoocha Subject: Unexpected merge/cascade behavior To: users@openjpa.apache.org Date: Wednesday, December 17, 2008, 7:22 AM Hi all, I'm quite new to (Open)JPA and am experiencing unexpected behavior of the merge() operation. I'm dealing with a parent and child entity that are defined as follows: //Parent: @Entity @Table(name=3D"TEST_PARENT") public class TestParent { =09@Id =09@Column(name=3D"ID", nullable=3Dfalse) =09private String id =3D new String(); =09 =09@Column(name=3D"name") =09private String name =3D new String(); =09 =09@OneToMany(mappedBy=3D"parent", fetch=3DFetchType.EAGER,=20 =09=09=09cascade=3D{CascadeType.ALL}) =09private Set children =3D new HashSet(); =09 =09@Override =09public boolean equals(Object other) { =09=09if (other !=3D null && other instanceof TestParent) { =09=09=09return ((TestParent)other).getId().equals(this.id); =09=09} =09=09 =09=09return false; =09} =09@Override =09public int hashCode() { =09=09return id.hashCode(); =09} =20 // skipping getters/setters =09 public void addChild(TestChild c) { =09=09this.children.add(c); =09=09c.setParent(this); =09} } //Child: @Entity @Table(name=3D"TEST_CHILD") public class TestChild { =09@Id =09@Column(name=3D"ID", nullable=3Dfalse) =09private String id =3D new String(); =09 =09@Column(name=3D"NAME") =09private String name =3D new String(); =09 =09@ManyToOne(cascade=3D{CascadeType.ALL}) =09@JoinColumn(name=3D"PARENT_ID", nullable=3Dfalse) =09private TestParent parent =3D new TestParent(); =09 =09@Override =09public boolean equals(Object other) { =09=09if (other !=3D null && other instanceof TestChild) { =09=09=09return ((TestChild)other).getId().equals(this.id); =09=09} =09=09 =09=09return false; =09} =09@Override =09public int hashCode() { =09=09return id.hashCode(); =09} =09//Skipping getters/setters } The class testing the merge() behavior looks as follows: public class PCTester { =09private EntityManager em =3D null; =09 =09public PCTester() { =09=09EntityManagerFactory emf =3D Persistence.createEntityManagerFactory( =09=09"testFactory"); =09=09em =3D emf.createEntityManager(); =09} =09 =09public void mergeParent(TestParent p) { =09=09em.getTransaction().begin(); =09=09p =3D em.merge(p); =09=09em.getTransaction().commit(); =09} =09public static void main(String[] args) { =09=09TestParent p =3D new TestParent(); =09=09p.setId("1"); =09=09p.setName("parent-1-update"); =09=09 =09=09TestChild c =3D new TestChild(); =09=09c.setId("1"); =09=09c.setName("child-1-update"); =09=09 =09=09p.addChild(c); =09=09 =09=09PCTester t =3D new PCTester(); =09=09t.mergeParent(p); =09} } Finally, the entity manager is configured as follows in persistence.xml: =09 org.apache.openjpa.persistence.PersistenceProviderImpl =09=09=20 =09=09com.lmco.jsf.alis.obphm.afrs.session.model.TestParent =09=09com.lmco.jsf.alis.obphm.afrs.session.model.TestChild =09=09 =09=09 =09=09=09 =09=09=09 =09=09=09 =09=09=09 =09=09=09 =09=09=09 =09=09 =09=09 =09=09 In case the database already contains parent and child records with id "1", the test runs without problems and both parent and child names are correctl= y updated. The SQL trace shows the following statements being executed: SELECT t0.NAME, t1.PARENT_ID, t1.ID, t1.NAME=20 FROM AFRS_USER.TEST_PARENT t0, AFRS_USER.TEST_CHILD t1=20 WHERE t0.ID =3D ? AND t0.ID =3D t1.PARENT_ID(+)=20 ORDER BY t1.PARENT_ID ASC [params=3D(String) 1] UPDATE AFRS_USER.TEST_PARENT=20 SET NAME =3D ?=20 WHERE ID =3D ?=20 [params=3D(String) parent-1-update, (String) 1] UPDATE AFRS_USER.TEST_CHILD=20 SET PARENT_ID =3D ?, NAME =3D ?=20 WHERE ID =3D ?=20 [params=3D(String) 1, (String) child-1-update, (String) 1] However, when parent and child do not exist in the database, the merge() operation throws an exception because the database tries to insert NULL int= o the parent table for reasons unknown to me: SELECT t0.NAME, t1.PARENT_ID, t1.ID, t1.NAME=20 FROM AFRS_USER.TEST_PARENT t0, AFRS_USER.TEST_CHILD t1=20 WHERE t0.ID =3D ? AND t0.ID =3D t1.PARENT_ID(+)=20 ORDER BY t1.PARENT_ID ASC=20 [params=3D(String) 1] SELECT t0.NAME, t1.ID, t1.NAME=20 FROM AFRS_USER.TEST_CHILD t0, AFRS_USER.TEST_PARENT t1=20 WHERE t0.ID =3D ? AND t0.PARENT_ID =3D t1.ID(+)=20 [params=3D(String) 1] // Unexpected!! SELECT t0.NAME, t1.PARENT_ID, t1.ID, t1.NAME=20 FROM AFRS_USER.TEST_PARENT t0, AFRS_USER.TEST_CHILD t1=20 WHERE t0.ID =3D ? AND t0.ID =3D t1.PARENT_ID(+)=20 ORDER BY t1.PARENT_ID ASC=20 [params=3D(String) ] INSERT INTO AFRS_USER.TEST_PARENT (ID, NAME)=20 VALUES (?, ?)=20 [params=3D(String) 1, (String) parent-1-update] INSERT INTO AFRS_USER.TEST_CHILD (ID, PARENT_ID, NAME)=20 VALUES (?, ?, ?)=20 [params=3D(String) 1, (String) 1, (String) child-1-update] // Unexpected!! INSERT INTO AFRS_USER.TEST_PARENT (ID, NAME)=20 VALUES (?, ?)=20 [params=3D(String) , (String) ] Leading to Exception in thread "main" org.apache.openjpa.persistence.RollbackException: The transaction has been rolled back. See the nested exceptions for details on the errors that occurred. Etc... When creating a new parent only (i.e. without children) in the test method, it is correctly persisted into the database with the merge() operation, so it seems that something goes wrong with cascading the merge to the child object. Does anyone know what's the reason for the unexpected SQL statements? Any help is highly appreciated. Thanks, Jonatan Samoocha Using: OpenJPA 1.0.0 JDK 1.5.0_14 Oracle 10G --=20 View this message in context: http://n2.nabble.com/Unexpected-merge-cascade-behavior-tp1668355p1668355.ht= ml Sent from the OpenJPA Users mailing list archive at Nabble.com. =0A=0A=0A --0-411714691-1229628616=:52755--