cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Malcolm Edgar" <malcolm.ed...@gmail.com>
Subject Re: problems with association table...
Date Wed, 05 Apr 2006 12:35:34 GMT
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 <cris.daniluk@gmail.com> wrote:
> There's two ways to do this - if the newsdocs table doesn't have any fields
> at all other than the doc_id and news_id, you don't even need to create an
> ObjEntity for it (you still need a DbEntity though). Just create a flattened
> relationship in the ObjEntity for News that goes to document (documentArray)
> and crosses the newsdoc DbEntity. Create the same in reverse. Then you can
> do:
>
> news.addToDocumentArray(doc);
>
> If you don't want to do this for whatever reason, usually because you have
> other data in the newsdocs table like a sort order, etc, try:
>
> News newsObj = (News) dataContext.createAndRegisterNewObject(News.class);
> Document docObj = (Document) dataContext.createAndRegisterNewObject(
> Document.class);
> NewsDocument newsDocObj = (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 commits.
> 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're
> doing and why when doing that :)
>
> Cris
>
> On 4/5/06, I. Venuti <i.venuti@caribel.it> wrote:
> >
> > Hi,
> >
> > I have a table (news) where I put some infos. I'd like to associate some
> > 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 = 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
> >
> >
>
>

Mime
View raw message