NHibernate - 添加到Collection的New Item不会持久化

时间:2009-09-21 10:56:23

标签: c# nhibernate castle-windsor

我有一个与联系人有一对多关系的客户表。我已经将NHibernate的两个类映射为

在客户端类

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
 assembly="MyAssembly.Core"
 namespace="MyAssembly.Core.Domain"
>
<class name="Client" table="CompanyXClients">
<id name="ClientId" column="ClientId" unsaved-value="0">
<generator class="identity" />
</id>
<property name="CompanyId" column="CompanyId" type="Int32" length="8" />
<property name="ClientName" column="ClientName" type="String" length="100" />
<property name="Address1" column="Address1" type="String" length="255" />
<property name="Address2" column="Address2" type="String" length="255" />
<property name="City" column="City" type="String" length="50" />
<property name="State" column="State" type="String" length="50" />
<property name="CountryCode" column="CountryCode" type="String" length="2" />
<property name="Zip" column="Zip" type="String" length="7" />
<property name="Phone" column="Phone" type="String" length="20" />
<property name="Fax" column="Fax" type="String" length="20" />
<property name="EmailAddress" column="EmailAddress" type="String" length="255" />
<property name="PayPalId" column="PayPalId" type="String" length="255" />
<property name="Notes" column="Notes" type="String" length="255" />
<property name="TimeZone" column="TimeZone" type="Decimal" />
<set name="ContactPersons" lazy="true" cascade="all">
<key column="ClientId" not-null="true"/>
<one-to-many class="ContactPerson"/>
</set>
</class>

</hibernate-mapping>

在ContactPerson类中

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
 assembly="MyAssembly.Core"
 namespace="MyAssembly.Core.Domain"
>
<class name="ContactPerson" table="ClientXContactPersons">
<id name="ContactPersonId" column="ContactPersonId" unsaved-value="0" type="Int64">
<generator class="identity" />
</id>
<property name="ClientId" column="ClientId" type="Int64" length="8"  insert="false" update="false" />
<property name="FirstName" column="FirstName" type="String" length="50" />
<property name="LastName" column="LastName" type="String" length="50" />
<property name="Designation" column="Designation" type="String" length="50" />
<property name="Mobile" column="Mobile" type="String" length="20" />
<property name="Phone" column="Phone" type="String" length="20" />
<property name="EmailAddress" column="EmailAddress" type="String" length="255" />
<property name="IsDefault" column="IsDefault" type="Boolean" />
<many-to-one name="Client" column="ClientId" not-null="true"/>
</class>

</hibernate-mapping>

我正在使用温莎城堡进行NHibernate。以下是保存客户的代码。

IClientDAO oClientDao = Factory.GetClientDAO();
Client oClient = null;
if (Form[this.ControlPrefix + "." + "ClientId"] != "0")
{
     oClient = oClientDao.GetById(long.Parse(Form["ClientId"]),true);
}
else
{
     oClient = new Client();
}
this.UpdateModel(oClient,"SomePrefix",Form.ToValueProvider());
oClient.CompanyId = AuthUser.CompanyId;
oClientDao.SaveOrUpdate(oClient);

我为ClientId(ContactPerson中的外键)设置了insert =“false”update =“false”,以阻止NHibernate在插入语句中生成额外列。

问题是模型从FormCollection中正确更新。如果我添加新客户端,当我更新客户端并且级联工作正常时,联系人也会被保留。但是,如果我编辑现有客户端,它会加载现有的联系人,从FormCollection更新数据。但更新客户端不会级联并执行联系人更新。因此,新联系人永远不会被持久化,甚至不会保存现有联系人的数据更改。

如果插入正常,并在更新客户端时级联到Contactpersons。什么一定是更新问题?

我还在正在执行的集合的setter中设置集合中Contact Person对象的Client Property。

public virtual ISet<ContactPerson> ContactPersons
 {
    get { return _ContactPersons; }
        set
        {
            _ContactPersons = value;
            foreach (ContactPerson oPerson in _ContactPersons)
            {
                oPerson.Client = this;
                oPerson.ClientId = this.ClientId;
            }
        }
    }

我在网上遇到的帖子中尝试过所有内容。现在对此感到沮丧。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:-1)

我不确定这是否是问题,但是我们遇到了类似的问题,我们发现每当我们使用通用的IList集合时,级联机制都无效。所以我们使用了非泛型集合