-->
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: NHibernate one-to-many relation problem
PostPosted: Sun May 14, 2006 8:58 am 
Newbie

Joined: Sun May 14, 2006 8:48 am
Posts: 12
You see this post very beatiful at http://hassan.behzadian.com/nhibernate-problem.htm
Hi.
I have a problem using NHibernate.
I have a class named Parent, this class contains a (property) collection (of type Iesi.Collections.ISet - ) named Children that contains references to list of other object type named Child. also child has a property named Parent that is a reference to its parent object. below is their definitions:
namespace NHT
{
public class Parent
{
private string title;
public string Title
{
get
{
return title;
}
set
{
title = value;
}
}

private Guid id;
public Guid Id
{
get
{
return id;
}
set
{
id = value;
}
}

private ISet children = new Iesi.Collections.ListSet();
public ISet Children
{
get
{
return children;
}
set
{
children = value;
}
}
}
}
namespace NHT
{
public class Child
{
private string title;
public string Title
{
get
{
return title;
}
set
{
title = value;
}
}

private Guid id;
public Guid Id
{
get
{
return id;
}
set
{
id = value;
}
}

private Parent parent;
public Parent Parent
{
get
{
return parent;
}
set
{
parent = value;
}
}
}
}

I mapped them to database like below
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="NHT.Parent, NHT" table="ORM_Parent">
<id name="Id" column="ParentId" type="System.Guid" unsaved-value="">
<generator class="guid" />
</id>
<property name="Title" type="string" />
<set name="Children" >
<key column="ParentId"/>
<one-to-many class="NHT.Child, NHT"/>
</set>
</class>
</hibernate-mapping>
----------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="NHT.Child, NHT" table="ORM_Child">
<id name="Id" column="ChildId" type="System.Guid" unsaved-value="">
<generator class="guid" />
</id>
<property name="Title" column="title" type="string"></property>
<many-to-one name="Parent" class="NHT.Parent, NHT" column="ParentId" />
</class>
</hibernate-mapping>
I write below code to test it,
-------------------
Parent parent = new Parent();
parent.Title = "Parent Title";
for (int i = 0; i < 15; i++) {
HibernateTestClassLibrary.Child child = new HibernateTestClassLibrary.Child();
child.Title = "Child Title:" + i;
parent.Children.Add(child);
child.Parent = parent;
//DatabaseManager.CurrentSession.Save(content);
}

object key = DatabaseManager.CurrentSession.Save(parent);
DatabaseManager.CurrentSession.Flush();

Response.Write("Object saved:" + key.ToString());
--------------------------------

but when executed give below error message:

-----------------
SQL insert, update or delete failed (expected affected row count: 1, actual affected row count: 0). Possible causes: the row was modified or deleted by another user, or a trigger is reporting misleading row count.
-----------------

I monitored Sql Server, Nhibernate first inserts parent and tries to update child object (in database), but because no child was saved (and then no records will be changed), throws above exception if i uncomment
//DatabaseManager.CurrentSession.Save(content);
all things will be true (child objects will be saved and updated by NHibernate) but i did not sure to get object loaded correctly if i change set in parent class like below
<set name="Children" invers="true" >
<key column="ParentId"/>
<one-to-many class="NHT.Child, NHT"/>
</set>

and NHibernate will tries to update Child objects then no error will be made. Now my question how i can configure NHibernate to save all child objects just by saving parent object?


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 14, 2006 2:24 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
set the cascade attribute in your collection:

Code:
cascade="save-update"


see the documentation for other values that may be more appropriate.

you should end up wih someth lik:

Code:
<set name="Children" cascade="save-update" inverse=true>
   <key column="ParentId"/>
   <one-to-many class="NHT.Child, NHT"/>
</set>


-devon


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.