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?