Return-Path: Delivered-To: apmail-incubator-cayenne-user-archive@locus.apache.org Received: (qmail 19181 invoked from network); 5 Apr 2006 12:35:56 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 5 Apr 2006 12:35:56 -0000 Received: (qmail 23569 invoked by uid 500); 5 Apr 2006 12:35:55 -0000 Delivered-To: apmail-incubator-cayenne-user-archive@incubator.apache.org Received: (qmail 23553 invoked by uid 500); 5 Apr 2006 12:35:55 -0000 Mailing-List: contact cayenne-user-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: cayenne-user@incubator.apache.org Delivered-To: mailing list cayenne-user@incubator.apache.org Received: (qmail 23544 invoked by uid 99); 5 Apr 2006 12:35:55 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 05 Apr 2006 05:35:55 -0700 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: domain of malcolm.edgar@gmail.com designates 64.233.162.196 as permitted sender) Received: from [64.233.162.196] (HELO zproxy.gmail.com) (64.233.162.196) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 05 Apr 2006 05:35:54 -0700 Received: by zproxy.gmail.com with SMTP id 13so2242242nzp for ; Wed, 05 Apr 2006 05:35:34 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=F9L1X7JtOyp8rrsSFZ0PcvA/LI1vYZ4XtAyt85M+EMmUIW+Qeq5CN+P5mF76FxFZ1XpV8OsUtUlTt1Kx0C/6QnJWJ7G7yvIij7t2HdktbBvwLRCZtM+ZKUlL5XRQZLnA4TjiaQltB4tUKa7ShasXQYb2MZoMIcLj29Yv6AbdkJ4= Received: by 10.64.241.19 with SMTP id o19mr1309712qbh; Wed, 05 Apr 2006 05:35:34 -0700 (PDT) Received: by 10.64.243.2 with HTTP; Wed, 5 Apr 2006 05:35:34 -0700 (PDT) Message-ID: <525d8e10604050535y2ec21ad3r66e77d77fb6cc3cc@mail.gmail.com> Date: Wed, 5 Apr 2006 22:35:34 +1000 From: "Malcolm Edgar" To: cayenne-user@incubator.apache.org Subject: Re: problems with association table... In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline References: <4433B419.3050609@caribel.it> X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Hi Ian, the NPE is probably due to the object not having a DataContext defined when you add the related object to it. regards Malcolm Edgar On 4/5/06, Cris Daniluk wrote: > There's two ways to do this - if the newsdocs table doesn't have any fiel= ds > at all other than the doc_id and news_id, you don't even need to create a= n > ObjEntity for it (you still need a DbEntity though). Just create a flatte= ned > relationship in the ObjEntity for News that goes to document (documentArr= ay) > and crosses the newsdoc DbEntity. Create the same in reverse. Then you ca= n > do: > > news.addToDocumentArray(doc); > > If you don't want to do this for whatever reason, usually because you hav= e > other data in the newsdocs table like a sort order, etc, try: > > News newsObj =3D (News) dataContext.createAndRegisterNewObject(News.class= ); > Document docObj =3D (Document) dataContext.createAndRegisterNewObject( > Document.class); > NewsDocument newsDocObj =3D (NewsDocument) > dataContext.createAndRegisterNewObject(NewsDocument.class); > > // set other properties > > newsDocObj.setToNews(newsObj); > newsDocObj.setToDocument(docObj); > > dataContext.commitChanges(); > > Cayenne will automatically resolve the primary keys for you when it commi= ts. > There is no need to commit twice. By the way, you are asking for trouble > when you create a DataObject via constructor. Be sure you know what you'r= e > doing and why when doing that :) > > Cris > > On 4/5/06, I. Venuti wrote: > > > > Hi, > > > > I have a table (news) where I put some infos. I'd like to associate som= e > > documents to these news (maybe none). These documents resides in the > > table documents. > > I've created an association table called newsdocs, where there are the > > two id's: one for news and one for documents. > > > > The modeler recorgnises the relashionships (infact the object Newsdocs > > has only thwo relationships: toDocuments, toNews. > > Also in the object News there is an array (newsdocsArray) and in the > > object Documents (newsdocsArray). > > > > How can I create a relation between a news and a document? > > > > I have tried: > > > > 1) created News: nw1 (via createANdRegisterNewObject) > > 2) created Documents: doc2 (via createANdRegisterNewObject) > > 3) commit > > 4) created a black record for Newsdocs: > > > > Newsdocs ndoc =3D new Newsdocs(); > > ndoc.setDataContext(...) // the same used in (via > > createANdRegisterNewObject) > > ndoc.setToDocuments(doc2); //****** NullPointerException > > ndoc.setToNews(nw1); > > doc2.addToNewsdocsArray(ndoc); > > nw1.add(via createANdRegisterNewObject) > > > > 5) commit > > > > Unfortunatly this doesn't work: there is a NullPointerException in ****= ** > > > > Maybe I need to understand better the use of relashionships. Any > > suggestion is wellcome! > > Thanks a lot > > > > -- Ivan > > > > > >