-->
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.  [ 6 posts ] 
Author Message
 Post subject: Can I force update and insert on a bidirectional one-to-one?
PostPosted: Thu May 10, 2007 9:58 am 
Newbie

Joined: Wed Oct 18, 2006 5:54 am
Posts: 7
A have a bidirectional one-to-one mapping with a foreign key.

<class table="person">
<one-to-one name="car" lazy="false" cascade="all"/>
....
</class>


<class table="car" lazy="false">
<many-to-one not-null="true" insert="false" unique="true"
column="person_id"
update="false" name="person"/>
....
</class>


Person p = new Person();
Car c = new Car();
p.setCar(c);
session.saveOrUpdate(person);

By here everything is Ok and all the id's are automatically set by hibernate.
The problem comes when I am trying to flush it to the database.
session.flush();

There is an exception that I can not put a null value for the person_id in Car. Shouldn't hibernate take care of this. Works well for many-to-one mapping. Maybe the problem is that there are no update and insert tags for one-to-one (I am setting from the other side of the foreign key)

Maybe some of you have had a similar problem, Thanks Zarko


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 10, 2007 5:35 pm 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
You're mixing one-to-one and one-to-many. If you want a true Hibernate style one-to-one, you need to use shared primary keys and the "foreign" ID generator:

http://www.hibernate.org/hib_docs/v3/re ... n-onetoone

-Chris

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 11, 2007 5:15 am 
Newbie

Joined: Wed Oct 18, 2006 5:54 am
Posts: 7
Thanks, but I need to use the foreign key.

Anyway I solved the problem by deleting cascade="all" on the one-to-one end of the mapping. I saw this on the link you sent me.


Zarko


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 11, 2007 5:51 am 
Newbie

Joined: Wed Oct 18, 2006 5:54 am
Posts: 7
Back to square one. I need cascade="all" so that the changes can take place on the referenced table.

Anyone has the same problem?

p.s. I must use one-to-one with the foreign key, not with the primary


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 11, 2007 9:12 am 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
Then use many-to-one with unique="true". That defines a foreign key relationship with multiplicity of 1.

-Chris

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 14, 2007 2:44 am 
Newbie

Joined: Wed Oct 18, 2006 5:54 am
Posts: 7
:) I did. It does not work. Look at the first post.


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