-->
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.  [ 3 posts ] 
Author Message
 Post subject: One-To-Many rel. from FK to composite unique constraint ?
PostPosted: Tue Feb 10, 2009 6:53 am 
Newbie

Joined: Tue Feb 10, 2009 4:54 am
Posts: 2
Hi all,

i have used Hibernate successfully in a couple of projects but now i am lost and need help.
a part of a ( unfortunately unchangeable ) db schema looks like this:

service
=====
tech_id ( primary_key )
service_id ( unique_constraint_1 )
service_version ( unique_constraint_1 )
...

service_details
==========
tech_id ( primary_key )
service_id ( foreign_key_1 )
service_version ( foreign_key_1 )
...

AFAIK Hibernate does support:
- composite primary keys
- a FK relationship to a (single column, non pk) unique constraint

But we want to map a composite foreign key to a (non primary key) composite unique constraint.

Would anyone - may be even someone from the hibernate team ... christian, gavin ... - please be so kind, to make a definite statement, if such a mapping is possible with hibernate (annotations) or not.

Thanks for helping


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 10, 2009 7:48 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I think it is possible. In our project we have a one-to-one relation that uses multiple non-primary key columns. Given that one-to-one relations usually have more restrictions than many-to-one associations I think it is possible.

Since we are only using xml mapping files in our project, I can only give you xml examples. Hopefully you will be able to convert it to annotations.

1. In your 'service' class you have to group together the properties that makes up the unique constraint. This is done with the <properties> mapping.

Code:
<properties name="service">
  <property name="service_id" ...>
  <property name="service_version" ...>
</properties>


2. In your 'service_details' class you have to do include the two foreign key columns in one <many-to-one> mapping:

Code:
<many-to-one
   name="service"
   property-ref="service"
  ....
  >
  <column name="service_id" ... />
  <column name="service_version" ... />
</many-to-one>


The above should give you a uni-directional association from service_details to service. We are not mapping the inverse direction in our project, so I don't how easy/hard it is.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 10, 2009 11:49 am 
Newbie

Joined: Tue Feb 10, 2009 4:54 am
Posts: 2
Thanks for your response.
Unfortunately there is no property/property-ref annotation.
I played around with the @JoinColumn(s) annotation without success until now.

Any other idea welcome. Thanks in advance


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.