-->
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.  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Pattern for associating a row with one of several tables?
PostPosted: Thu Apr 29, 2004 6:40 pm 
Regular
Regular

Joined: Wed Sep 03, 2003 9:56 pm
Posts: 58
I have an 'Issue' object/table that represents a problem in our system. I'd like to be able to associate Issue objects with one of 5 different other objects/tables.

One approach would be to have 5 join tables, one per source object/table.

Another approach would be to have my Issues table have 5 reference columns, of which only one is used at a time. Each reference column would be a foreign key to one of the associated 'source' tables.

Neither of these solutions seem optimal to me. Does anyone have an idea on other/better solutions that I haven't considered yet?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 29, 2004 6:42 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
There is no other mechanism than a key copy and a foreign key constraint in the relational model, and this is a good thing.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 30, 2004 9:47 am 
Regular
Regular

Joined: Wed Sep 03, 2003 9:56 pm
Posts: 58
Can you explain what you mean by 'key copy'?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 30, 2004 10:03 am 
Regular
Regular

Joined: Tue Aug 26, 2003 3:09 pm
Posts: 58
Couldn't you use an <any> mapping? The relational model is not optimal because you need two columns: one indicates the table to join to, the other is the key (but you obviously can't have a foreign key constraint). Check the reference docs.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 30, 2004 10:25 am 
Regular
Regular

Joined: Wed Sep 03, 2003 9:56 pm
Posts: 58
I'm not sure.

I just re-read section 5.2.5 'Any Type Mappings' in the Hibernate documentation and I'm still not sure what <any/> type mappings are all about (sorry).

It sounds like it *might* provide what I'm looking for, but it also states that "it is impossible to specify a foriegn key constraint for this kind of association". I'm not sure I need a foreign key constraint, it just seems like sound database design practice.

Can someone provide a more throrough definition of the <any/> type mapping, or perhaps an example?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 30, 2004 10:43 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You need an <any> mapping if you have an association where the target is mapped with a table per concret class mapping. Thats all you have to know, if you don't have one of those, don't bother.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 30, 2004 10:54 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Entities in the relational model have not pointer associations, such as in Java, but share a key value. In most cases, this "copy of a key value" is protected with a foreign key constraint, to ensure referential integrity and to protect the semantics of your entity relationships.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 30, 2004 11:29 am 
Regular
Regular

Joined: Wed Sep 03, 2003 9:56 pm
Posts: 58
So 'key copy' is synonymous with 'foreign key'?

If this is the case, is it reasonable to have multiple foriegn keys in a single table, where only one of which will be used at a time? This means that the other (unused) foreign key columns will be null.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 30, 2004 11:31 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
I think you should read up on relational database fundamentals.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 30, 2004 11:32 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
And: The <any> is never a good idea, it's a workaround for legacy data models.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 30, 2004 11:52 am 
Regular
Regular

Joined: Wed Sep 03, 2003 9:56 pm
Posts: 58
Got it.

So either:

o It's standard practice to have multiple PKs but use only one, and I don't know it.

or

o It's a violoation of standard practice to have multiple PKs but use only one, and I don't know it.

Given that I've spent over an hour with the Oracle 9i Complete Reference Manual and can't find one word regarding this issue, can you give me a hint which one you think is correct? :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 30, 2004 11:56 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
multiple PKs


what are you talking about? You can't have multiple primary keys. Do you mean multiple foreign keys? And in what sense multiple?? All to the same referenced table? Different tables?

Get a book about database design, not the Oracle manual.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 30, 2004 11:58 am 
Regular
Regular

Joined: Wed Sep 03, 2003 9:56 pm
Posts: 58
I'm sorry. I don't know how I got PK in there. I certainly meant FK.

Wrong book. That explains it. I'll look elsewhere.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 30, 2004 12:01 pm 
Regular
Regular

Joined: Wed Sep 03, 2003 9:56 pm
Posts: 58
My problem is that I have one table that I need to relate each row to exactly one of four other tables.

The goal is to associate problems within our system to multiple problem-sources.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 30, 2004 12:08 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
An <any> mapping is not unreasonable in this case. However, if I were you, I would make the associated objects all belong to a single <subclass> or <joined-subclass> inheritance hierarchy. Much more elegant overall design.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 16 posts ]  Go to page 1, 2  Next

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.