-->
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: Cascading problem with one-to-one
PostPosted: Tue Nov 14, 2006 3:47 am 
Newbie

Joined: Tue Nov 14, 2006 3:34 am
Posts: 2
Hi there.

I have a big problem with my <one-to-one> mapping that i've made.
I have a table Person and a table PersonLE
A Person can have one or none PersonLE, so i think it's an one-to-one relation.

Now the problem: When i generate a new Person and a new PersonLE, and then set Person.PersonLE = le, the object wouldn't be saved..
What's wrong in my hbm Files? Here the Codes:

Person.hbm.xml
Quote:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" default-lazy="true" >
<class name="Hbm.Person, Hbm" table="tblPerson">

<id name="Id" column="person_id" type="Int32" length="3" unsaved-value="0">
<generator class="increment" />
</id>

<property name="Name" column="person_name" type="String" length="50" not-null="true" />
<property name="FirstName" column="person_firstname" type="String" length="50" not-null="true" />
<property name="Address" column="person_address" type="String" not-null="true" />
<property name="Phone" column="person_phone" type="String" length="50" not-null="true" />
<property name="Email" column="person_email" type="String" length="70" not-null="true" />
<property name="Type" column="person_type" type="String" length="2" not-null="true" />
<property name="Unumber" column="person_unr" type="String" length="10" not-null="true" />

<one-to-one name="PersonLE" class="Hbm.PersonLE, Hbm" cascade="all" />

</class>
</hibernate-mapping>


PersonLE.hbm.xml
Quote:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" default-lazy="false" >
<class name="Hbm.PersonLE, Hbm" table="tblPersonLE">

<id name="Id" column="le_id" type="Int32" length="3" unsaved-value="0">
<generator class="foreign">
<param name="property">Person</param>
</generator>
</id>

<property name="PhonePrivat" column="le_privphone" type="String" length="15" not-null="true" />
<property name="Handy" column="le_handy" type="String" length="15" not-null="true" />
<property name="EmailPrivat" column="le_privmail" type="String" length="150" not-null="true" />
<property name="Gender" column="le_gender" type="String" length="1" not-null="false" />
<property name="Language" column="le_language" type="String" length="20" not-null="false" />
<property name="BirthDate" column="le_birthdate" type="DateTime" not-null="false" />
<property name="Ahv" column="le_ahv" type="String" length="50" not-null="true" />
<property name="School" column="le_school" type="Int32" not-null="false" />
<property name="SchoolDay1" column="le_schoolday_1" type="Int32" length="1" not-null="true" />
<property name="SchoolDay2" column="le_schoolday_2" type="Int32" length="1" not-null="true" />
<property name="Bms" column="le_bms" type="Int32" length="1" not-null="true" />
<property name="Train" column="le_train" type="Int32" length="1" not-null="true" />

<one-to-one name="Person" class="Hbm.Person, Hbm" constrained="true"/>

</class>
</hibernate-mapping>


Person.cs
Quote:
public Person Person
{
get { return this.person; }
set { this.person = value; }
}


PersonLE.cs
Quote:
public virtual PersonLE PersonLE
{
get { return personLe; }
set { personLe = value; }
}


The testcase
Quote:
BaseDataAccess bda = new BaseDataAccess();

Person user = new Person();
user.Name = "Test";
user.FirstName = "Test";
user.Email = "Test";
user.Address = "Test";
user.Type = "LE";
user.Phone = "393";
user.Unumber = "u33923323";

PersonLE le = new PersonLE();
le.Gender = "m";
le.Ahv = "0000";
le.BirthDate = DateTime.Now;
le.Bms = 0;
le.EmailPrivat = "test";
le.Handy = "00";
le.Language = "Deutsch";
le.PhonePrivat = "000";
le.School = 1;
le.SchoolDay1 = 3;
le.SchoolDay2 = 4;
le.Train = 1;

user.PersonLE = le;

bda.Save(user);
//bda.Save(le);
BaseDataAccess.CloseSession();


In this case i become an error that says the object couldn't be saved. This because the cascade=all in the Person.hbm.xml. Without this, the Person object would be saved, but the PersonLE not.

When i get a Person object, the PersonLE loads too. That is ok. Is it possible that my logic is false with the link between the two objects in the testcase?

Can anyone help me by my problem?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 14, 2006 9:07 am 
Newbie

Joined: Tue Nov 14, 2006 3:34 am
Posts: 2
After many Hours reading different homepages and searching in this forum i found the answer :D

Instead of

user.PersonLE = le;

write

le.Person = user;

Now i think i know the tricks of nhibernate and can write my application :)


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.