Return-Path: Delivered-To: apmail-openjpa-users-archive@minotaur.apache.org Received: (qmail 84023 invoked from network); 30 Mar 2009 17:26:45 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 30 Mar 2009 17:26:45 -0000 Received: (qmail 58949 invoked by uid 500); 30 Mar 2009 17:26:45 -0000 Delivered-To: apmail-openjpa-users-archive@openjpa.apache.org Received: (qmail 58886 invoked by uid 500); 30 Mar 2009 17:26:45 -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 58876 invoked by uid 99); 30 Mar 2009 17:26:45 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 30 Mar 2009 17:26:45 +0000 X-ASF-Spam-Status: No, hits=0.2 required=10.0 tests=SPF_HELO_PASS,SPF_PASS,WHOIS_MYPRIVREG X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of lists+1214986160035-208411@n2.nabble.com designates 216.139.236.158 as permitted sender) Received: from [216.139.236.158] (HELO kuber.nabble.com) (216.139.236.158) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 30 Mar 2009 17:26:38 +0000 Received: from tervel.nabble.com ([192.168.236.150]) by kuber.nabble.com with esmtp (Exim 4.63) (envelope-from ) id 1LoLFZ-0001pB-Ro for users@openjpa.apache.org; Mon, 30 Mar 2009 10:26:17 -0700 Message-ID: <1238433977855-2558574.post@n2.nabble.com> Date: Mon, 30 Mar 2009 10:26:17 -0700 (PDT) From: Nemanja Joksovic To: users@openjpa.apache.org Subject: Re: Identity class and parent/children entity relationship In-Reply-To: <399081.4873.qm@web55908.mail.re3.yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Nabble-From: joksovicn@gmail.com References: <1238329990357-2552430.post@n2.nabble.com> <102740.94592.qm@web55902.mail.re3.yahoo.com> <1238432206456-2558373.post@n2.nabble.com> <399081.4873.qm@web55908.mail.re3.yahoo.com> X-Virus-Checked: Checked by ClamAV on apache.org Thanks Fay, I tried it before and it's working fine. But in production environment I need to using stable OpenJPA 1.2.x version (JPA 1.0 specification). Regards, Nemanja J. The workaround is to use MappedById annotation (JPA 2.0 spec) in Openjpa trunk: @Entity public class Child implements Serializable { @EmbeddedId @Column(name = "id", unique = false, nullable = false) private ChildId childId; @MappedById("parent") @ManyToOne @JoinColumn(name = "parent_id", referencedColumnName = "id") private Parent parent; ... } @Embeddable public class ChildId { public long id; public long parent; ...} @Entity public class Parent { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", unique = true, insertable = false, updatable = false, nullable = false) private long id; @OneToMany(cascade = CascadeType.ALL, mappedBy = "parent") private List children = new ArrayList(); private String name; ...} For more detail, please see org.apache.openjpa.persistence.enhance.identity.TestMappedById.testMappedById5 -Fay --- On Mon, 3/30/09, Nemanja Joksovic wrote: > From: Nemanja Joksovic > Subject: Re: Identity class and parent/children entity relationship > To: users@openjpa.apache.org > Date: Monday, March 30, 2009, 9:56 AM > Thank you very much. > > Nemanja J. > > > > The second problem is a known problem. I will open a JIRA > for it. > > > --- On Sun, 3/29/09, Nemanja Joksovic > wrote: > > > From: Nemanja Joksovic > > Subject: Identity class and parent/children entity > relationship > > To: users@openjpa.apache.org > > Date: Sunday, March 29, 2009, 5:33 AM > > Hi all, > > > > I've been experiencing few problems with Identity > class > > and Parent/Children entity relationship. I tried with > both > > OpenJPA 1.2.1 and 2.0.0-SNAPSHOT. I have a very simple > test > > case which can briefly explain problems: > > > > @Entity > > public class Parent implements Serializable { > > > > private long id; > > .... > > private List children = new > > LinkedList(); > > > > @Id > > // @GeneratedValue(strategy = > GenerationType.IDENTITY) > > @Column(name = "id", unique = true, > > insertable = false, updatable = false, nullable = > false) > > public long getId() { > > return id; > > } > > > > public void setId(long id) { > > this.id = id; > > } > > > > ... > > > > @OneToMany(cascade = CascadeType.ALL, mappedBy = > > "parent") > > public List getChildren() { > > return children; > > } > > > > public void addChild(Child child) { > > if (child == null) { > > throw new > IllegalArgumentException("Cannot > > add a null Child"); > > } > > this.getChildren().add(child); > > } > > > > public void setChildren(List > children) { > > this.children = children; > > } > > } > > > > @Entity > > @IdClass(ChildId.class) > > public class Child implements Serializable { > > > > private long id; > > ... > > private Parent parent; > > > > @Id > > @Column(name = "id", unique = false, > nullable > > = false) > > public long getId() { > > return id; > > } > > > > public void setId(long id) { > > this.id = id; > > } > > > > ... > > > > @Id > > @ManyToOne > > @JoinColumn(name = "parent_id", > > referencedColumnName = "id") > > public Parent getParent() { > > return parent; > > } > > > > public void setParent(Parent parent) { > > this.parent = parent; > > } > > } > > > > ChidId is generated with the Application Identity > Tool: > > > > public class ChildId implements Serializable { > > ... > > public long id; > > public long parent; > > ... > > } > > > > 1) Merge operation fail in any case with following > > exception: > > > > java.lang.NullPointerException > > at > > > net.company.persistence.Child.pcCopyKeyFieldsFromObjectId(Child.java) > > at > > > net.company.persistence.Child.pcNewInstance(Child.java) > > at > > > org.apache.openjpa.kernel.AttachStrategy.persist(AttachStrategy.java:93) > > at > > > org.apache.openjpa.kernel.VersionAttachStrategy.attach(VersionAttachStrategy.java:100) > > at > > > org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java:241) > > at > > > org.apache.openjpa.kernel.AttachStrategy.attachCollection(AttachStrategy.java:333) > > at > > > org.apache.openjpa.kernel.AttachStrategy.replaceList(AttachStrategy.java:359) > > at > > > org.apache.openjpa.kernel.AttachStrategy.attachField(AttachStrategy.java:223) > > at > > > org.apache.openjpa.kernel.VersionAttachStrategy.attach(VersionAttachStrategy.java:153) > > at > > > org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java:241) > > at > > > org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java:101) > > at > > > org.apache.openjpa.kernel.BrokerImpl.attach(BrokerImpl.java:3212) > > ... 25 more > > > > > > The problem is caused by the call > > pc.NewInstance(AttachStrategy:93) with null state > manager > > instance which is used in > pcCopyKeyFieldsFromObjectId(..): > > > > > pcsetParent((Parent)pcStateManager.getPCPrimaryKey(childid, > > 2 + pcInheritedFieldCount)); > > > > Also, for same problem exists JIRA Issue: > > https://issues.apache.org/jira/browse/OPENJPA-218 > > > > > > 2) Persist working fine, but it's fail in the case > when > > Parent.id is Generated Value field with following > exception: > > > > > > org.apache.openjpa.persistence.InvalidStateException: > > Detected reentrant flush. Make sure your flush-time > > instance callback methods or event listeners do not > invoke > > any operations that require the in-progress flush to > > complete. > > at > > > org.apache.openjpa.kernel.BrokerImpl.flushSafe(BrokerImpl.java:1904) > > at > > > org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:1679) > > at > > > org.apache.openjpa.kernel.StateManagerImpl.assignObjectId(StateManagerImpl.java:524) > > at > > > org.apache.openjpa.kernel.StateManagerImpl.assignObjectId(StateManagerImpl.java:506) > > at > > > org.apache.openjpa.kernel.StateManagerImpl.fetchObjectId(StateManagerImpl.java:1434) > > at > > > net.company.persistence.Parent.pcFetchObjectId(Parent.java) > > at > > > net.company.persistence.Child.pcCopyKeyFieldsToObjectId(Child.java) > > > > > > Does exists some workaround for these problems ? > > > > Regards, > > Nemanja J. > > -- > > View this message in context: > > > http://n2.nabble.com/Identity-class-and-parent-children-entity-relationship-tp2552430p2552430.html > > Sent from the OpenJPA Users mailing list archive at > > Nabble.com. > > > > > > > -- > View this message in context: > http://n2.nabble.com/Identity-class-and-parent-children-entity-relationship-tp2552430p2558373.html > Sent from the OpenJPA Users mailing list archive at > Nabble.com. -- View this message in context: http://n2.nabble.com/Identity-class-and-parent-children-entity-relationship-tp2552430p2558574.html Sent from the OpenJPA Users mailing list archive at Nabble.com.