Return-Path: Delivered-To: apmail-openjpa-users-archive@minotaur.apache.org Received: (qmail 56514 invoked from network); 13 Nov 2009 17:27:46 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 13 Nov 2009 17:27:46 -0000 Received: (qmail 42113 invoked by uid 500); 13 Nov 2009 17:27:46 -0000 Delivered-To: apmail-openjpa-users-archive@openjpa.apache.org Received: (qmail 42076 invoked by uid 500); 13 Nov 2009 17:27:46 -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 42066 invoked by uid 99); 13 Nov 2009 17:27:46 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 13 Nov 2009 17:27:46 +0000 X-ASF-Spam-Status: No, hits=-2.6 required=5.0 tests=BAYES_00,HTML_MESSAGE X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of michael.d.dick@gmail.com designates 209.85.218.209 as permitted sender) Received: from [209.85.218.209] (HELO mail-bw0-f209.google.com) (209.85.218.209) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 13 Nov 2009 17:27:43 +0000 Received: by bwz1 with SMTP id 1so3747086bwz.33 for ; Fri, 13 Nov 2009 09:27:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :from:date:message-id:subject:to:content-type; bh=i9dWkQ607Q4XNimudgnOQxh3SAAEx/MfmeU150Ky/ms=; b=RouP4I/SW6jQ0hBF8AsvhnHaYTzHzSKKc9ZuKH2jOYOqQ555GXDZ7RmNVHqNDEr6ik hxI9NJRywsd9vA7TWh33trohx/+d3OZnDFYpxHIllH/N7rakq8yhTAmS5nU1wK89ag12 xdAP6xAEPg04Ucz7b3JkIgBLFQ2VP+c1BHKnY= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; b=mtavGTCM0jpsGUB0KzQV/hLTWS4LcQzwFuEwMa1jylknGH+aLT8FxlmqUUMDN6oeqy TVUjEb0EOwMIqv4VK/l3yPNS3LIgI28fJi1w2GOattmqnQnVu9h4NGtmRPS+1dDBfUaO lylkHdh/HvNy8uJQpWU8h1zh+AbG/4DFwYEOU= MIME-Version: 1.0 Received: by 10.204.141.18 with SMTP id k18mr1621058bku.139.1258133240131; Fri, 13 Nov 2009 09:27:20 -0800 (PST) In-Reply-To: <8FDAD303-D834-4756-B10E-322C95E4BFB4@logemann.org> References: <72c1350f0911130752y302234edr13cdc543f5c88baa@mail.gmail.com> <72c1350f0911130817m5867487y683d580bfa0bccf6@mail.gmail.com> <8FDAD303-D834-4756-B10E-322C95E4BFB4@logemann.org> From: Michael Dick Date: Fri, 13 Nov 2009 11:27:00 -0600 Message-ID: <72c1350f0911130927n56f478cdvd4906b556077a0@mail.gmail.com> Subject: Re: inverse OneToMany relation - getting weird To: users@openjpa.apache.org Content-Type: multipart/alternative; boundary=0015175cb728dbb073047843f896 --0015175cb728dbb073047843f896 Content-Type: text/plain; charset=ISO-8859-1 Hi Marc, Are you setting the relationship for both the Father and Child entities? I see the behavior you describe if I do this : Father f; Child c; for (int i = 0; i < nFathers; i++) { f = new Father(); f.setChildren(new ArrayList()); em.persist(f); for(int j = 0 ; j < nChildren; j++ ) { c = new Child(); // c.setFather(f); f.getChildren().add(c); em.persist(c); } Uncommenting c.setFather(f); fixes the problem. If that doesn't help let me know which version of OpenJPA and which database you're using and I'll try to recreate. -mike On Fri, Nov 13, 2009 at 10:33 AM, Marc Logemann wrote: > Hi, > > what you have done works of course but thats not my problem because thats > what i had before i wanted to do the inverse. Your example simply > demonstrates how to use ElementJoinColumn and i am pretty aware of that ;-) > The problem starts when using mappedBy on the children inside Father. > > When i am doing it as you described it in your email before, i am at least > not getting any errors back but instead he inserts a "null" in my FK field. > > @ManyToOne > @ForeignKey > @JoinColumn(name="team_oid", referencedColumnName="oid") > public Team team; > > I am really clueless. It simply cant be so hard and uncommon to make an > inverse OneToMany with a FK field in the child table. > > I googled for hours and all example i ve found just dont work. > > > > --- > regards > Marc Logemann > http://www.logemann.org > http://www.logentis.de > > > > > Am 13.11.2009 um 17:17 schrieb Michael Dick: > > > Hi Marc, >> >> Did some more reading and I was wrong about the use case for the @Element >> annotations. The @Element annotations allow you to specify the FK >> constraint >> on the One side of a @OneToMany, so your annotations would look like this >> : >> >> @Entity >> public class Father extends Person { >> @OneToMany >> @ElementForeignKey >> @ElementJoinColumn(name="father_oid", referencedColumnName="oid") >> Collection children; >> . . . >> } >> >> @Entity >> public class Child extends Person { >> @ManyToOne >> private Father father; >> . . . >> } >> >> It appears to be functionally identical to using @JoinColumn and >> @ForeignKey >> on the Child class, but the documentation is a bit sparse on this >> annotation.. >> >> There are several examples in our unit tests though. For example >> >> openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/mapping/bidi/ParentWithAutoIdentity.java >> looks somewhat similar to your model. >> >> Hope this helps, >> -mike >> >> >> ---------- Forwarded message ---------- >> From: Michael Dick >> Date: Fri, Nov 13, 2009 at 9:52 AM >> Subject: Re: inverse OneToMany relation - getting weird >> To: users@openjpa.apache.org >> >> >> Hi Marc, >> >> I think the @ElementForeignKey and @ElementJoinColumn annotations are >> intended to be used with non-entity types (ie PersistentCollections). >> >> If Child is an entity you'd want to use @ForeignKey and @JoinColumn >> instead >> (I gave this a quick try and it looks like it worked for me). >> >> Regards, >> -mike >> >> >> On Fri, Nov 13, 2009 at 7:03 AM, Marc Logemann wrote: >> >> Hi, >>> >>> after struggling for several hours, i need to ask for help. >>> >>> Following scenario..... >>> >>> DB Table: Father >>> --------------------------- >>> INT oid >>> .... >>> >>> >>> DB Table: Childs >>> ------------------------ >>> INT oid >>> INT father_oid >>> ... >>> >>> Java Entity: Father >>> -------------------------- >>> ... >>> @OneToMany(cascade = CascadeType.PERSIST, mappedBy = "father") >>> private List childList; >>> >>> Java Entity: Child >>> -------------------------- >>> ... >>> @ManyToOne(cascade = CascadeType.PERSIST) >>> @ElementForeignKey >>> @ElementJoinColumn(name="father_oid", referencedColumnName="oid") >>> protected Father father; >>> >>> >>> When i try to persist a Father, i am getting: >>> >>> >>> org.apache.openjpa.persistence.ArgumentException: Field >>> "Father.childList" >>> declares "Child.father" as its mapped-by field, but this field is not a >>> direct relation. at...... >>> >>> Ok, then i changed Child to: >>> >>> Java Entity: Child >>> -------------------------- >>> ... >>> @ManyToOne(cascade = CascadeType.PERSIST) >>> @Column(name = "father_oid") >>> protected Father father; >>> >>> >>> When i now do persist a Father object, both DB records (Father and >>> Childs) >>> are filled with records but the FK column in Child (father_oid) is empty. >>> So >>> the FK relation is broken but it did persist childs, which is somehow >>> weird. >>> >>> So whatever i do, i never get a fully persisted graph with correct FK >>> values. >>> >>> Can anyone give me a hint what i am doing wrong here? >>> >>> >>> Thanks a lot. >>> >>> --- >>> regards >>> Marc Logemann >>> http://www.logemann.org >>> http://www.logentis.de >>> >>> >>> >>> >>> >>> > --0015175cb728dbb073047843f896--