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.  [ 4 posts ] 
Author Message
 Post subject: one-to-one cascade update
PostPosted: Tue May 27, 2008 10:57 am 
Newbie

Joined: Tue May 27, 2008 10:43 am
Posts: 4
Hibernate version:1.2.1

Well, I`ve a mapping file for classes Person & Adress, and they are interconnected as one-to-one
<-------------------------------------------------------------->
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="HibernateLayer" namespace="HibernateLayer.Mappings">
<class name="Person" table="Person" lazy="false">
<id name="Id">
<column name="PersonId" not-null="true"/>
<generator class ="increment" />
</id>
<one-to-one class="Adress" name="Adress" property-ref="Person" lazy="false" cascade="all"/>
</class>
</hibernate-mapping>

<-------------------------------------------------------------->
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="HibernateLayer" namespace="HibernateLayer.Mappings">
<class name="Adress" table="Adress" lazy="false">
<id name="Id" column="Id" access="field.pascalcase-underscore">
<generator class="increment">
</generator>
</id>
<many-to-one name="Person" class="Person" column="PersonId" lazy="false" cascade="all" foreign-key="FK_Adress_Person" unique="true"/>
</class>
</hibernate-mapping>

<-------------------------------------------------------------->

But when I`m doing following:
Code:
...
Person p = Person.Get(Id);
Adress a = new Adress();
p.Adress = a;
p.Update();


new Adress object is written to the database, but field PersonId (which is foreign key) is empty (null)

what is wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 12:05 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I think, you also have to set a.Person:

a.Person = p

or is this done in p.Address = a ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 28, 2008 5:51 am 
Newbie

Joined: Tue May 27, 2008 10:43 am
Posts: 4
Yep. When I use a.Person = p everythng works fine, but I`m interested is there any possibility to avoid this assignment?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 28, 2008 5:57 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
If you want the id set during insert (which you need due to the foreign key), I don't think so.

_________________
--Wolfgang


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