Hibernate version:
1.0.2.0
Mapping documents:
Parent.hbm.xml
<? xml version="1.0" encoding="utf-8" ?>
< hibernate-mapping xmlns ="urn:nhibernate-mapping-2.0" >
< class name ="HYLQ.Core.Domain.Parent, HYLQ.Core" table ="Parent" >
< id name ="Id" column ="Id" unsaved-value ="0" >
< generator class ="assigned" />
</ id >
< property name ="Title" type ="String" length ="200" />

< bag name ="Childs" lazy ="true" table ="Child" inverse ="true" cascade ="all"
access ="NHibernate.Generics.GenericAccessor, NHibernate.Generics" >
< key column ="pid" />
< one-to-many class ="HYLQ.Core.Domain.Child, HYLQ.Core" />
</ bag >
</ class >
</ hibernate-mapping >
Child.hbm.xml
<? xml version="1.0" encoding="utf-8" ?>
< hibernate-mapping xmlns ="urn:nhibernate-mapping-2.0" >
< class name ="HYLQ.Core.Domain.Child, HYLQ.Core" table ="Child" >
< id name ="Id" column ="Id" unsaved-value ="0" >
< generator class ="assigned" />
</ id >
< property name ="Memo" type ="String" length ="200" />
< many-to-one name ="Parent" column ="pid" class ="HYLQ.Core.Domain.Parent, HYLQ.Core"
access ="NHibernate.Generics.GenericAccessor, NHibernate.Generics" />
</ class >
</ hibernate-mapping >
测试代码:
using (ISession session = TestCategory.Factory.OpenSession())

{
int id = 1;

Parent parent = new Parent();

parent.Id = id;
parent.Title = "tetetet";

Child child = new Child();
child.Id = 1;
child.Memo = "222";
child.parent = parent;

parent.Childs.Add(child);

ITransaction trans = session.BeginTransaction();

try

{
session.Save(parent);
trans.Commit();
}
catch

{
trans.Rollback();
throw;
}
}
当我设置了
chilid.Id =1的时候,则出现了
Test method TestProject1.TestCategory.AddParentChild threw exception: NHibernate.HibernateException: 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..
跟踪执行的SQL语句后,发现并不是Insert Child,而是Update Child。
当我将chilid.Id =1注释后,就正常,不过chilid.Id他用 unsaved-value="0"代替。
执行的SQL语句都是正常的。
而我在网上看到一些One2Many的例子中其中表的主键都是自增长的后非assigned的。
所以这个问题很是困惑,最后,自己写了一个自定义的Generator来生产ID,
后来就没有出现上述的情况了。
你们碰到过这种情况吗?
1.0.2.0
Mapping documents:
Parent.hbm.xml
















Child.hbm.xml











测试代码:



































当我设置了
chilid.Id =1的时候,则出现了
Test method TestProject1.TestCategory.AddParentChild threw exception: NHibernate.HibernateException: 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..
跟踪执行的SQL语句后,发现并不是Insert Child,而是Update Child。
当我将chilid.Id =1注释后,就正常,不过chilid.Id他用 unsaved-value="0"代替。
执行的SQL语句都是正常的。
而我在网上看到一些One2Many的例子中其中表的主键都是自增长的后非assigned的。
所以这个问题很是困惑,最后,自己写了一个自定义的Generator来生产ID,
后来就没有出现上述的情况了。
你们碰到过这种情况吗?