-->
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: Mapping two tables where Forign key is not the primary key
PostPosted: Wed Nov 23, 2005 10:56 am 
Newbie

Joined: Wed Nov 23, 2005 10:51 am
Posts: 1
I am working in a legacy application where the Foriegn Key in Table B is not the primary key of Table A. There is a one-to-many relationship between Table A and B.

How can I define this relationship in Hibernate ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 12:59 pm 
Regular
Regular

Joined: Sat Nov 05, 2005 5:33 am
Posts: 70
Location: Linz, Austria
Read 8.4.1. one to many / many to one. I think this should be exactly what you need.

_________________
hth,
Heinz
Don't forget to rate if this helped


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 2:43 am 
Beginner
Beginner

Joined: Fri Jun 10, 2005 5:26 am
Posts: 25
Location: Adelaide, South Australia
hhuber wrote:
Read 8.4.1. one to many / many to one. I think this should be exactly what you need.


Actually, no - the one-to-many and many-to-one mappings here use exactly primary key fields and not foreign key fields.

I'm intrigued too. How can I map a relationship, say it's many to one using foreign keys in table A to fields in table B that aren't part of B's primary key either - and I'm talking about multiple fields?

The reference documentation doesn't really give any examples of this, and given that the reference docs are driven by example, it makes it rather difficult.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 2:57 am 
Regular
Regular

Joined: Sat Nov 05, 2005 5:33 am
Posts: 70
Location: Linz, Austria
damomurf wrote:
hhuber wrote:
Read 8.4.1. one to many / many to one. I think this should be exactly what you need.


Actually, no - the one-to-many and many-to-one mappings here use exactly primary key fields and not foreign key fields.

I'm intrigued too. How can I map a relationship, say it's many to one using foreign keys in table A to fields in table B that aren't part of B's primary key either - and I'm talking about multiple fields?

The reference documentation doesn't really give any examples of this, and given that the reference docs are driven by example, it makes it rather difficult.


You can specify a property-ref on the many-to-one.
See 6.1.10 many-to-one.
On the master side, you have to group the properties using properties. This way you can refer to multiple properties by one name.

I've got no clue though, how you'd do this on the one-to-many side, since you don't use the primary key of the master to join.
The only thing I know is that you have to use column elements below your key element (or a property-ref again):
Code:
<key >
    <column name="detailCol1" />
    <column name="detailCol2" />
</key>

_________________
hth,
Heinz
Don't forget to rate if this helped


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 3:48 am 
Beginner
Beginner

Joined: Fri Jun 10, 2005 5:26 am
Posts: 25
Location: Adelaide, South Australia
hhuber wrote:
On the master side, you have to group the properties using properties. This way you can refer to multiple properties by one name.


Brilliant - that was exactly what I was after.

Many thanks - if I could rate your response I would :)


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.