我在父映射文件中使用nHibernate(3.3.3GA)并发版本标记。
1,当我更新我的子集合(.add或.remove)时,父版本不会更新。
2,当我更新子实体上的属性时,父版本不会更新。
我唯一能够更新父版本的是我在父实体上编辑属性的时候。
有人可以看看是否有遗漏或做错的事情吗?
我有以下代码:
父类:
[DataContract]
public class Parent
{
public Parent()
{
this.Id = -1;
this.Children = new List<Child>();
}
[DataMember(Name = "Id")]
public int Id { get; set; }
[DataMember(Name = "Name")]
public string Name { get; set; }
[DataMember(Name = "Children")]
public ILIst<Child> Children { get; set; }
[DataMember(Name = "Deleted")]
public bool Deleted { get; set; }
[DataMember(Name = "Version")]
public byte[] Version { get; set; }
public override string ToString()
{
return this.Name;
}
}
儿童班:
[DataContract]
public class Child
{
public Child()
{
this.Id = -1;
}
[DataMember(Name = "Id")]
public int Id { get; set; }
[DataMember(Name = "Parent")]
public Parent Parent { get; set; }
[DataMember(Name = "Name")]
public string Name { get; set; }
[DataMember(Name = "Deleted")]
public bool Deleted { get; set; }
public override string ToString()
{
return this.Name;
}
}
父映射:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="TestApp.Entities" auto-import="false">
<class name="TestApp.Entities.Parent" table="TestDb.dbo.ParentTable" dynamic-update="true" lazy="false" optimistic-lock="dirty">
<id name="Id" column="ParentID" type="int" unsaved-value="-1">
<generator class="native" />
</id>
<version name="Version" generated="always" type="BinaryBlob" unsaved-value="null">
<column name="Version" sql-type="timestamp" not-null="false" />
</version>
<property name="Name" column="Name" />
<bag name="Children" table="TestDb.dbo.ChildTable" lazy="false" inverse="true" where="Deleted = 0" fetch="join">
<key column="ParentID" />
<one-to-many class="TestApp.Entities.Child" />
</bag>
</class>
</hibernate-mapping>
子映射:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="TestApp.Entities" auto-import="false">
<class name="TestApp.Entities.Child" table="TestDb.dbo.ChildTable" dynamic-update="true" lazy="false">
<id name="Id" column="ChildID" type="int" unsaved-value="-1">
<generator class="native" />
</id>
<many-to-one name="Parent" class="TestApp.Entities.Parent" column="ParentID" cascade="none" fetch="join" />
<property name="Name" column="Name" />
</class>
</hibernate-mapping>
接下来我在父(.add或.remove)中编辑子集合并点击保存。父版本不会更新。接下来我尝试编辑子[0] .Name并点击保存,再次父版本不会更新。
我使用以下内容保存到db:
public void Merge<T>(ref T entity) where T : class
{
try
{
T updatedEntity = null;
using (ISession session = this.SessionFactory.OpenSession())
{
using (session.BeginTransaction())
{
updatedEntity = (T)session.Merge(entity);
session.Transaction.Commit();
}
}
entity = updatedEntity;
}
catch (StaleObjectStateException sose)
{
throw sose;
}
}
在编辑我的子集合时,是否有人能够找到导致我的父版本未更新的原因?
非常感谢答案 0 :(得分:1)
好的,在 Radim Kohler 的帮助下,我现在解决了这个问题。
将数据库中的“版本”列从timestamp
更改为datetime
,然后更改“版本”映射,如下所示:
在:
<version name="Version" generated="always" type="BinaryBlob" unsaved-value="null">
<column name="Version" sql-type="timestamp" not-null="false" />
</version>
后:
<version name="LastUpdated" type="DateTime" unsaved-value="null">
<column name="LastUpdated" sql-type="datetime" not-null="false" />
</version>
现在已经解决了这个问题。现在,当我使用.Add()
或.Remove()
更新我的子集合时,或者我在集合中编辑了一个或多个子项的属性时,我父项'LastUpdated'属性已更新。