-->
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.  [ 2 posts ] 
Author Message
 Post subject: Foreign key is missing by one-to-one association
PostPosted: Fri Sep 03, 2004 6:35 am 
Newbie

Joined: Fri Sep 03, 2004 6:24 am
Posts: 2
Hi,
i have a problem by saving a one-to-one association
I have two tables/classes: One and Two
One has the columns id and two_id (foreign key)
Two has the columns id and value

I want to save a object from One and the Two-object should be saved automaticly. It works, but the two_id in the table One is missing, after entrying.

One:
id two_id
1 NULL

Two
id value
1 First entry

I send my mapping files and the java files.
Can you detect what is wrong?


Hibernate version:
2.1.6

Mapping documents:

<class
name="hibernate.One"
table="one"
>

<id
name="id"
type="java.lang.Integer"
column="id"
>
<generator class="identity" />
</id>


<!-- Associations -->

<!-- bi-directional one-to-one association to Two -->
<one-to-one
name="two"
class="hibernate.Two"
cascade="all"
constrained="true"
/>


</class>
<class
name="hibernate.Two"
table="two"
>

<id
name="id"
type="java.lang.Integer"
column="id"
>

<generator class="identity"/>

</id>

<property
name="value"
type="java.lang.String"
column="value"
length="255"
/>

<!-- Associations -->


</class>


Code between sessionFactory.openSession() and session.close():

SessionFactory sessionFactory =
new Configuration().configure().buildSessionFactory();

Session ses = sessionFactory.openSession();
Transaction trans = ses.beginTransaction();

Two two = new Two();
two.setValue( "First entry" );


One one = new One();
one.setTwo( two );

ses.save( one );
// ses.save( two );


trans.commit();
ses.close();


Full stack trace of any exception that occurs:

Name and version of the database you are using:
mysql 4.1


I think the property two_id in the One-mapping is missing, but i don`t know how to use it.

Thanks a lot


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 03, 2004 12:53 pm 
Newbie

Joined: Fri Aug 20, 2004 8:26 am
Posts: 5
Location: Karlsruhe, Germany
The doc says:
".. there are two varieties of one-to-one associations:
1. primary key association
2. unique foreign key association ..."

So, if your association is the first one (and this is what your mapping looks like), you don't need a foreign key (extra table column) at all, as the two rows share the same primary key value.

If it's the second one, i.e. if your class Two has a property that references One, then your mapping should look like:

<class name="hibernate.One" table="one">

<id name="id" type="java.lang.Integer" column="id">
<generator class="identity" />
</id>

<!-- Associations -->
<many-to-one name="two" class="hibernate.Two" column="two_id" unique="true" cascade="all"/>

</class>
<class name="hibernate.Two" table="two">

<id name="id" type="java.lang.Integer" column="id">
<generator class="identity"/>
</id>

<one-to-one name="whatever your property's name is" class="hibernate.One" property-ref="two"/>

<property name="value" type="java.lang.String" column="value" length="255" />

<!-- Associations -->

</class>

This should work.
Alice


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