-->
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.  [ 11 posts ] 
Author Message
 Post subject: one-to-one not saving associated object... foreign key null
PostPosted: Fri Nov 18, 2005 3:58 am 
Newbie

Joined: Wed Sep 28, 2005 12:24 am
Posts: 15
Hibernate version: 3.0

Mapping documents:

I am trying to map a unidirectional one to one association between Person and Address (a person has one address) where the personId PK from Person is present as a FK in the Address table.

<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<one-to-one name="address"
class="org.compname.Address"
property-ref="personId"
/>
</class>

<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<property name="personId" column="personId" not-null="true" insert="false" update="false" />
</class>

create table Person ( personId bigint not null primary key)
create table Address ( addressId bigint not null primary key, personId bigint not null unique )


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

(using spring hibernate template)
getHibernateTemplate().saveOrUpdate(person);

Full stack trace of any exception that occurs:

Null id in entry. Cannot insert row with person_id null...

Name and version of the database you are using:
SQL Server 2000

The generated SQL (show_sql=true):

update Address set personId = null where id = ?


I am able to load up the address and update it but if i try to insert it i get the error that null not allowed in personId for the Address table. Its like its not carrying the id from the Person down into the child address object.

Help!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 4:01 am 
Regular
Regular

Joined: Sat Nov 05, 2005 5:33 am
Posts: 70
Location: Linz, Austria
I guess you have to tell Hibernate about the relationship in the class whose table has the fk.

_________________
hth,
Heinz
Don't forget to rate if this helped


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 4:29 am 
Newbie

Joined: Wed Sep 28, 2005 12:24 am
Posts: 15
so make it bidirectional then? See i dont want to do that... i also dont want to have a person object to the address class. I still dont get the need for bidirectional relationships. Maybe what I am trying to do it doesnt make sense for... maybe there are better examples for why its needed. I just dont get it..


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 4:35 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi

How about the below mapping?

Mapping for Parent <class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<one-to-one name="address" class="org.compname.Address" cascade="all"></one-to-one>
</class>

Mapping for Child
<class name="Address">
<id name="id" column="addressId">
<generator class="foreign">
<param name="property">personId</param>
</generator>
</id>
<one-to-one name="personId" class="org.compname.Person" constrained="true"></one-to-one>
</class>

For more info on unidirectional one-to-one association look in to this
http://www.hibernate.org/hib_docs/v3/re ... tional-121

And one Question?
Why do u use SaveOrUpdate when you clearly know that what action you have to perfom i.e) insert or update?

I think its wise to use SaveOrUpdate when you are not sure that the record is in the DataBase or not.

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 4:36 am 
Regular
Regular

Joined: Sat Nov 05, 2005 5:33 am
Posts: 70
Location: Linz, Austria
I read up in the docs (http://www.hibernate.org/hib_docs/v3/reference/en/html/associations.html#assoc-unidirectional-121).

I think you have to use a many-to-one with unique="true".
As far as I can tell, a unidirectional one-to-one only works for the table where the fk resides.
[/url]

_________________
hth,
Heinz
Don't forget to rate if this helped


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 4:55 am 
Newbie

Joined: Wed Sep 28, 2005 12:24 am
Posts: 15
i'll try tomorrow when i get to work.. thanks ramnath...

btw i'm using saveOrUpdate because i'm just prototyping... also i have an extremely large object map (40+ objects) that is multiple levels deep ... a bunch of one-to-many, one-to-one and components... Usually after manipulating several items in the object map i sent the top level parent to hibernate to persist. I want it to handle what has been changed/added... so even more reason to use saveOrUpdate.

now, having said that... if i'm doing a simple change to one object.. sure i'll just save just that one and call "update".


hhuber, i tried doing the many-to-one with the unique="true" and it still didnt work.. same error.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 4:27 pm 
Newbie

Joined: Wed Sep 28, 2005 12:24 am
Posts: 15
nope that didnt work. Any other ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 22, 2005 2:54 am 
Regular
Regular

Joined: Sat Nov 05, 2005 5:33 am
Posts: 70
Location: Linz, Austria
I looked back in the reference ([url=file:///c:/Program%20Files/dev/db/hibernate/hibernate-3.0/doc/reference/en/html/associations.html#assoc-unidirectional-12m]8.2.3. one to many[/url]). This seems to match your DDL.
The one-to-one (or many-to-one as I proposed before) have different DDL from yours.

The drawback is you have to use a set. But you could make the accessors for the set private and expose only the getter-setter interface for a single object.


We've got a simular table layout in our application. We made the relationship bidirectional with a one-to-one in the parent and a unique many-to-one in the child table.


Perhaps that helps,
Heinz


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 22, 2005 2:55 am 
Regular
Regular

Joined: Sat Nov 05, 2005 5:33 am
Posts: 70
Location: Linz, Austria
Sorry, the link is a local one. Here the correct link: http://www.hibernate.org/hib_docs/v3/reference/en/html/associations.html#assoc-unidirectional-12m


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 1:31 am 
Newbie

Joined: Fri Sep 09, 2005 11:40 am
Posts: 12
hhuber,

We are faced with the same problem - would seem that there may be a problem with the one-to-one associations and Hibernate performing all related inserts. So your using the one-to-one and a unique many-to-one in the child table essentially creates the one-to-one correct? You still had to use a set though, is that correct?

Michelle


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 2:36 am 
Regular
Regular

Joined: Sat Nov 05, 2005 5:33 am
Posts: 70
Location: Linz, Austria
Well, we've just started the development, so this far, we're only reading the database. But this works perfectly ;-)

We didn't have to use a set.
We have a nullable one-to-one association on the parent side.
The foreign key and the many-to-one are on the detail side.
Both associations are mode with the correct object. No need to use a set since we don't have any *-to-many.

We'll try inserting and updating soon. You'll probably hear from me (somewhere in the forum), if it doesn't work :-)

hth,
Heinz


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