Hi ,
I'm having an error saving or updating data with a parent - child persistent class. I try to save a parent table with assigned key. The child have a relation one-to-many and has also an not assigned id.
I suppose that if i want to save the parent and the chid with a unique instuction i should use Save. But this don't work.
Can anyone help me.
Thanks
Here is the code:
DB: Sqlserver 2005
Hibernate version: 1.0.2
// *************mapping hibernate***********************
<?xml version="1.0" encoding="utf-8"?>
<!--Generated from NHibernate.Mapping.Attributes on 2006-10-06 12:58:15Z.-->
<hibernate-mapping auto-import="true" xmlns="urn:nhibernate-mapping-2.0">
<class name="MyAssembly.Parent, MyAssembly" table="Parent" mutable="true">
<id name="ParentCode" type="String" unsaved-value="null">
<column name="ParentCode" length="20" not-null="true" />
<generator class="assigned" />
</id>
<bag name="Childs" cascade="all-delete-orphan">
<key column="ParentCode" />
<one-to-many class="MyAssembly.Child, MyAssembly" />
</bag>
</class>
<class name="MyAssembly.Child, MyAssembly" table="Promotion" mutable="true">
<id name="ChildCode" type="String" unsaved-value="null">
<column name="ChildCode" length="20" not-null="true" />
<generator class="assigned" />
</id>
<property name="Descrizione" type="String">
<column name="Descrizione" length="10" not-null="false" />
</property>
</class>
</hibernate-mapping>
// ****************classe model*******************************
using NHibernate.Mapping.Attributes;
using NHMA = NHibernate.Mapping.Attributes;
public class Parent
{
public Parent()
{
_childs = new ArrayList();
}
//Primary Key
private String _parentCode;
[NHMA.Id(0, Name = "ParentCode", Type = "String", UnsavedValue = "null")]
[NHMA.Column(1, Name = "ParentCode", Length = 20, NotNull = true)]
[NHMA.Generator(2, Class = "assigned")]
public String ParentCode
{
get { return _parentCode; }
set { _parentCode = value; }
}
private ArrayList _promotions;
[NHMA.Bag(0, Cascade = NHMA.CascadeStyle.AllDeleteOrphan)]
[NHMA.Key(1, Column = "ParentCode")]
[NHMA.OneToMany(2, Class = "MyAssembly.Child, MyAssembly")]
public IList Childs
{
get { return _childs; }
set { _childs = value; }
}
}
using NHibernate.Mapping.Attributes;
using NHMA = NHibernate.Mapping.Attributes;
public class Child
{
public Child()
{
}
//Child primary key
private String _childCode;
[NHMA.Id(0, Name = "ChildCode", Type = "String", UnsavedValue = "null")]
[NHMA.Column(1, Name = "ChildCode", Length = 20, NotNull = true)]
[NHMA.Generator(2, Class = "assigned")]
public String ChildCode
{
get { return _childCode; }
set { _childCode = value; }
}
private String _descrizione;
[NHMA.Property(0, Type = "String")]
[NHMA.Column(1, Name = "Descrizione", Length = 10, NotNull = false)]
public String Descrizione
{
get { return _descrizione; }
set { _descrizione = value; }
}
}
// *************Saving parent-child***********************
private int saveParentChilds()
{
try
{
Parent pr = new Parent();
pr.ParentCode = "P01";
Child ch = new Child();
ch.ChildCode = "C01";
ch.Descrizione = "desc1";
pr.Promotions.Add(ch);
//...
//.. inizializzazione ISessionFactory e ISession
//..
session = factory.OpenSession();
ITransaction tr = session.BeginTransaction();
try
{
session.Save(objectForSave);
tr.Commit();
res = 1;
}
catch (Exception ec)
{
tr.Rollback();
res = -1;
throw new Exception(MessageException);
}
finally
{
tr.Dispose();
if (closeSession) session.Close();
}
return res;
}
//*************Error*********************************
{"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."}
|