You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Tony Giaccone <tg...@gmail.com> on 2011/04/12 21:43:49 UTC

Mapping Question

This is more of a generic question, though I'm interested in how it might be
implemented in Cayenne as well.


I have a number of entities, Think Topic, Project, Category, Asset.  On each
one I need to model a to many relationship to Notes, Where a Note is an
object with a subject and a date created and content.

I"m not sure what the best strategy is to map each of these different
entities. I really don't want to create 4 note tables, So that there's a
Topic Note table, and a Project  Note table and  well you get the idea.

I could put a discriminator on the table, so that each entry has it's own
"type" that would solve part of the problem, but they I have to create the
foreign keys relationship.

So I'm just looking for some advice. Have you dealt with a similar
situation, and how did you model in general this solution and in Cayenne in
particular.

Thanks for the help


Tony Giaccone

Re: Mapping Question

Posted by Aristedes Maniatis <ar...@maniatis.org>.
On 13/04/11 5:43 AM, Tony Giaccone wrote:
> I have a number of entities, Think Topic, Project, Category, Asset.  On each
> one I need to model a to many relationship to Notes, Where a Note is an
> object with a subject and a date created and content.

Another approach is this:

DB tables:

Topic
Project
Asset
Note
NoteRelationship


Object Entities:

Topic
Project
Asset
Note
NoteTopicRelationship
NoteAssetRelationship
NoteProjectRelationship

Use inheritance to set this structure up in Cayenne. A discriminator column in the NoteRelationship will define how the joins work. In the modeler, put something like "entityIdentifier = 1"  into the NoteTopicRelationship and"entityIdentifier = 2" into the NoteAssetRelationship, etc

The advantage of this approach is that you get a many to many join and the Java code looks very readable and clear.


Ari



-- 
-------------------------->
Aristedes Maniatis
GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A

Re: Mapping Question

Posted by Tony Giaccone <tg...@gmail.com>.
Yeah, that was my first thought. Scary when you and I think alike.
Conceptually it's clear, and clean, but then you have this set of sparsely
populated foreign keys. After all only one of them is ever going to have a
value in it.    That idea just irks me, it seems wasteful. But really in
these days of terabyte drives should I even let that thought enter my mind.
 I think having grown up in a world where 8k of memory was all you had,
there's always a feeling of hating to waste bytes..  But that was 30+ years
ago,  gots to live in the present. :-)


On Wed, Apr 13, 2011 at 8:39 AM, Michael Gentry <mg...@masslight.net>wrote:

> In your Note table, you can have several foreign keys: thinkTopicFK,
> projectFK, categoryFK, assetFK.  Then map each FK appropriately, so
> your Note class ends up with a getThinkTopic(), getProject(),
> getCategory(), and getAsset() .  On the other side, you'll have
> getNotes() (in your ThinkTopic class, for example).  Of course, on the
> Note side, most of those will return empty lists, but I'm assuming
> you'll be drilling down from the other side?  If you care about going
> from Note back the other way, you may want a discriminator column or
> something to identify the type of note and use inheritance.
>
> mrg
>
>
> On Tue, Apr 12, 2011 at 3:43 PM, Tony Giaccone <tg...@gmail.com>
> wrote:
> > This is more of a generic question, though I'm interested in how it might
> be
> > implemented in Cayenne as well.
> >
> >
> > I have a number of entities, Think Topic, Project, Category, Asset.  On
> each
> > one I need to model a to many relationship to Notes, Where a Note is an
> > object with a subject and a date created and content.
> >
> > I"m not sure what the best strategy is to map each of these different
> > entities. I really don't want to create 4 note tables, So that there's a
> > Topic Note table, and a Project  Note table and  well you get the idea.
> >
> > I could put a discriminator on the table, so that each entry has it's own
> > "type" that would solve part of the problem, but they I have to create
> the
> > foreign keys relationship.
> >
> > So I'm just looking for some advice. Have you dealt with a similar
> > situation, and how did you model in general this solution and in Cayenne
> in
> > particular.
> >
> > Thanks for the help
> >
> >
> > Tony Giaccone
> >
>

Re: Mapping Question

Posted by Michael Gentry <mg...@masslight.net>.
In your Note table, you can have several foreign keys: thinkTopicFK,
projectFK, categoryFK, assetFK.  Then map each FK appropriately, so
your Note class ends up with a getThinkTopic(), getProject(),
getCategory(), and getAsset() .  On the other side, you'll have
getNotes() (in your ThinkTopic class, for example).  Of course, on the
Note side, most of those will return empty lists, but I'm assuming
you'll be drilling down from the other side?  If you care about going
from Note back the other way, you may want a discriminator column or
something to identify the type of note and use inheritance.

mrg


On Tue, Apr 12, 2011 at 3:43 PM, Tony Giaccone <tg...@gmail.com> wrote:
> This is more of a generic question, though I'm interested in how it might be
> implemented in Cayenne as well.
>
>
> I have a number of entities, Think Topic, Project, Category, Asset.  On each
> one I need to model a to many relationship to Notes, Where a Note is an
> object with a subject and a date created and content.
>
> I"m not sure what the best strategy is to map each of these different
> entities. I really don't want to create 4 note tables, So that there's a
> Topic Note table, and a Project  Note table and  well you get the idea.
>
> I could put a discriminator on the table, so that each entry has it's own
> "type" that would solve part of the problem, but they I have to create the
> foreign keys relationship.
>
> So I'm just looking for some advice. Have you dealt with a similar
> situation, and how did you model in general this solution and in Cayenne in
> particular.
>
> Thanks for the help
>
>
> Tony Giaccone
>

Re: Mapping Question

Posted by "Hans C. Poo" <ha...@welinux.cl>.
Tony,

I had a similar problem and solved it making the relationship in the reverse sense of what composition dictates.

First create NoteCollection that contains a Collection of Notes.

Entities, Think Topic, Project, Category, Asset must have an "id_note_collection" field. 

Example:

Project --> NoteCollection --> * Note
Asset --> NoteCollection --> * Note

Any Entity that needs a NoteCollection must have an noteCollectionId.

Bye
Hans

----- Mensaje original -----
De: "Tony Giaccone" <tg...@gmail.com>
Para: user@cayenne.apache.org
Enviados: Martes, 12 de Abril 2011 16:43:49
Asunto: Mapping Question

This is more of a generic question, though I'm interested in how it might be
implemented in Cayenne as well.


I have a number of entities, Think Topic, Project, Category, Asset.  On each
one I need to model a to many relationship to Notes, Where a Note is an
object with a subject and a date created and content.

I"m not sure what the best strategy is to map each of these different
entities. I really don't want to create 4 note tables, So that there's a
Topic Note table, and a Project  Note table and  well you get the idea.

I could put a discriminator on the table, so that each entry has it's own
"type" that would solve part of the problem, but they I have to create the
foreign keys relationship.

So I'm just looking for some advice. Have you dealt with a similar
situation, and how did you model in general this solution and in Cayenne in
particular.

Thanks for the help


Tony Giaccone