-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Relationship mapping question
PostPosted: Fri May 18, 2007 11:50 am 
Newbie

Joined: Thu Feb 09, 2006 12:37 pm
Posts: 18
Hi all,

I have the following tables:

Code:
[b]Note[/b]
id (PK)
text

[b]BookNote[/b]
noteID
bookID

[b]CdNote[/b]
noteID (PK)
cdID (PK)

[b]Book[/b]
id (PK)
otherfields

[b]Cd[/b]
id (PK)
other fields


I am trying to model that a book, can have many notes but but I note only belongs to one book (your usual one-to-many) however in my wisedom I decided that a note coulde belong to a CD, and a CD can have many notes (again one-to-many), but a note will only ever belong to one book or one cd never both.

I am just wondering how to map this, would it be correct to modely it as a one-to-many relationship between the book and note, and, CD and note? would that work with the assoication table inbetween, i dont think it will.

Or should I use uni directional many-to-many relationship, as I am not intrested in the note knowing what it belongs, so long as teh book and cd acan find all their notes?

Of course in teh future my users might decided they want this functionality, I can solves this be ensuring that a note can only be added to one CD or one book on teh forms :D

G


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 19, 2007 4:21 am 
Beginner
Beginner

Joined: Fri Jul 22, 2005 3:35 pm
Posts: 24
Location: Buenos Aires, Argentina
When modeling things for using with Hibernate it's useful to think about an Object model, not just tables. Here, you can define a hierarchy, both Books and Notes are Things. Thing would be a superclass. The note would have a relationship with Thing. This inheritance would be mapped in one of the standard inheritance mapping schemes Hibernate provides. E.g. like this:

Thing:
thing_id (pk)

Book:
thing_id (pk and fk to Thing)

CD:
thing_id (pk and fk to Thing)

Note:
note_id (pk)
thing_id (fk to Thing)
text

For each record in both book and CD there would be a record in Thing. If you have any data which is common to both CDs and Books (like title) it could be in the superclass Thing, i.e. in that table.

Check the other inheritance schemes, which may be better for your taste.


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 19, 2007 7:59 am 
Newbie

Joined: Thu Feb 09, 2006 12:37 pm
Posts: 18
Would that map to the tables I described?
I am not sure I would know how to model that relationship in the DB, but thanks for the advice, I will check it out, anything to make my life easier.

thanks
G


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 19, 2007 1:22 pm 
Beginner
Beginner

Joined: Fri Jul 22, 2005 3:35 pm
Posts: 24
Location: Buenos Aires, Argentina
No, it would talke an extra table. But you can also use other inheritance strategies. In particular, the "union-subclass". With that strategy Hibernate will use a table for Book, other for Cd and no table for Thing. It will set up things so that the key-space between the tables is shared.

Check the documentation at http://www.hibernate.org/hib_docs/v3/re ... tance.html .

And remember to rate these posts if they help! =)


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 20, 2007 1:42 pm 
Newbie

Joined: Thu Feb 09, 2006 12:37 pm
Posts: 18
I think I will go for table per concreate class and make the class implement a "marker" interface, for now at least, I am only worried about the fact that in my application, there is actual a parent child relations ship between "book" and "CD".

Are all one to many relationships mapped with set? (that is the only example i have seen) is the a list? or is that nothing to do with the Set and List as found in the collections API?

Thanks for your help.
Rating the thread ;)

Gavin


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.