-->
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: one FK that can have PK's from more than one other table?
PostPosted: Tue Oct 11, 2005 1:43 pm 
Newbie

Joined: Fri Oct 07, 2005 2:21 pm
Posts: 4
Location: New Brunswick, Canada
Is it possible to have a single fk in a table that is a pk from more than one table?

IE, you have a demographics table, user table, and a company table.

The demographics table contains info about both the user and the company, and instead of having both a userId and CompanyId foreign keys, could we have one field that might be either the companyId or the userId? And have another field that would identify the demographic type?

How would you implement that?

thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 11, 2005 2:31 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 2:46 pm
Posts: 45
Location: Saskatoon SK Canada
I think you'll find that there aren't any databases that support creating a foreign key that references more than one primary key (although you could fake it by manually doing the validation in a trigger).

I would do the following:
- Remove the userId and companyId from the demographics table.
- Add a couple of tables: person_demographics and company_demographics. Each table would have two 'id' columns, one a foreign key to the demographics table, and the other a foreign key to the respective type table (i.e. person or company).

If you still want a single demographics table that you can query without having to worring about the new tables, then create a view.

_________________
Maury


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 11, 2005 2:46 pm 
Newbie

Joined: Fri Oct 07, 2005 2:21 pm
Posts: 4
Location: New Brunswick, Canada
Actually I was able to create two imported keys in the table in MySQL. And they both reference the same ownerId field.

My problem is writing the annotations to have hibernate generate the tables and the persistence.

If I only had to do this all in sql and db scripts, I'd be done by now, but I'm trying to understand hibernate. :-)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 5:58 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
do a HQL query to join your objects and do not map the relationship.
The relationship you describe can be mapped in Hibernate through <any> but it requires an extra discr column and has some conceptual/perf limitations. Plus it is not mappable through annotations so far (only hbm files)

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Re: one FK that can have PK's from more than one other table
PostPosted: Fri Oct 14, 2005 1:12 pm 
Newbie

Joined: Fri Oct 14, 2005 11:40 am
Posts: 7
I have a somewhat similar situation; trying to create a CMA which associates metadata with data in an existing database, probably a database other than the one where the metadata is stored and which may be legacy, so may have composite primary keys,

The solution I've come up with so far is to use the fact that Hibernate likes to use a class to make up composite keys. So I have a "serializable" column on my metadata table which takes a serialized object, and an "entity" column that takes the name of the entity. Then I can simply do a session.load(entity, serializable) to retrieve the data.

It's nice in that it imposes no limits on the legacy database; but I haven't got as far as working out what the cost is in performance terms. Potentially quite a lot, I fear!


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.