-->
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.  [ 1 post ] 
Author Message
 Post subject: one-to-one mapping with a shared composite primary key
PostPosted: Thu Oct 02, 2008 10:14 am 
Newbie

Joined: Fri Aug 03, 2007 9:39 am
Posts: 2
Hi all,
I'm facing a mapping problem with a legacy database. I have a table with a composite primary key (two columns) in 1:1 relation with another table.

For primary key of a single value the problem is solved, as you can see also in this example by "Java Persistence with Hibernate", section 7.1.1: we have a USER table in 1:1 relation with a ADDRESS table, and primary key of ADDRESS table is a foreign key equal to the USER table primary key.

Here the user:

Code:
<class name="User" table="USER">
  <id name="id" column="USER_ID">
    <generator class="native" />
  </id>
...
  <one-to-one name="shippingAddress"
                     class="Address"
                     cascade="save-update"/>
</class>
public class User {
...
private Address shippingAddress;
// Getters and setters
}


and here the address:
Code:
<class name="Address" table="ADDRESS">
   <id name="id" column="ADDRESS_ID">
      <generator class="foreign">
         <param name="property">user</param>
      </generator>
   </id>
...
   <one-to-one name="user"
                       class="User"
                     constrained="true"/>
</class>

public class Address {
...
private User user;
// Getters and setters
}


But what if USER primary key is a composite key?

I tried to solve in this way:

Code:
<class name="User" table="USER">
  <composite-id name="id" class="UserId">
             <key-property name="name"
                       column="USER_NAME" />
            <key-property name="surname"
                       column="USER_SURNAME" />
  </composite-id>
...
  <one-to-one name="shippingAddress"
                     class="Address"
                     cascade="save-update"/>
</class>

<class name="Address" table="ADDRESS">
   <id name="id">
   <column name="USER_NAME" />
   <column name="USER_SURNAME" />
      <generator class="foreign">
         <param name="property">user</param>
      </generator>
   </id>
...
   <one-to-one name="user"
                       class="User"
                     constrained="true"/>
</class>




where UserId is a class representing the composite key, but this is clearly wrong.

Is there a way to do this?


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

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.