Hi,
I have a problem mapping a one-to-one association using a foreign key.
The association I want to map is described as follow : a User has one shipping Adress and an Adress can be associated to only one User.
I want to map a unidirectionnal association from User to Adress using the foreign key strategy (i.e using a "unique constraint").
Here is the mapping of User :
Code:
<hibernate-mapping>
<class name="User" table="USER">
<id name="id" type="java.lang.Long">
<column name="USER_ID" />
<generator class="native" />
</id>
<property name="login" type="java.lang.String">
<column name="LOGIN" />
</property>
<many-to-one name="shippingAdress" class="Adress" [b]unique="true"[/b] not-null="true" cascade="all">
<column name="SHIPPING_ADRESS_ID" />
</many-to-one>
</class>
</hibernate-mapping>
The trouble is that when I export this mapping to DDL , no unique constraint is created for the SHIPPING_ADRESS_ID. Therefore code like below works:
Code:
Adresse adress = new Adresse();
adress.setZipCode("94230");
User u1 = new User();
u1.setLogin("foo");
u1.setShippingAdress(adress);
User u2 = new User();
u2.setLogin("bar");
u2.setShippingAdress(adress);
session.save(u1);
session.save(u2);
As you can see an Adress can be associated with two different users which is not the semantic I want.
Is someone see what is wrong with my mapping as I am desperate.
Here is the configuration of my application :
- the version of Hibernate : 3.5.4-Final
- the datebase : MySQL 5 (innoDB). I have tried another database (Apache derby) and the problem is still the same.
- the JDBC driver : com.mysql.jdbc.Driver.
Thanks in advance.