-->
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-one relationship using a foreign key.[RESOLVED]
PostPosted: Thu Aug 05, 2010 8:30 am 
Newbie

Joined: Mon Jul 28, 2008 12:55 pm
Posts: 3
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.


Last edited by hektoumayo on Fri Aug 06, 2010 1:50 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: One-to-one relationship using a foreign key.
PostPosted: Thu Aug 05, 2010 8:52 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I think you may need to specify the unique="true" on the <column> tag instead of the <many-to-one> tag. Or you could skip the <column> tag and use <many-to-one column="..."> instead.


Top
 Profile  
 
 Post subject: Re: One-to-one relationship using a foreign key.
PostPosted: Fri Aug 06, 2010 1:49 pm 
Newbie

Joined: Mon Jul 28, 2008 12:55 pm
Posts: 3
Thanks man it works...


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.