更新父母和子女记录不起作用

时间:2009-07-28 14:06:08

标签: nhibernate

我设置了父子关系表。在UI中,我显示了父级和子级详细信息的详细信息。用户在UI中进行更改后,我需要将其保存回数据库,但我收到以下错误:

  

NHibernate.ADOException被捕获

     

消息=“无法更新:[PlanningMaps.vo.PaRegAddresses#2] [SQL:UPDATE PaRegAddresses SET RegUserId =?,AddressCity =?,AddressCountry =?,AddressLine =?,AddressLine1 =?,AddressPostcode =?,RegUserId =?,IsBillingAddress = ?, IsShippingAddress =?WHERE RegAddressID =?]“

和innerexception是:

  

System.Data.SqlClient.SqlException:列名'RegUserId'在结果列列表中出现多次。

以下是映射详细信息: 的 PARENT:

<?xml version="1.0" encoding="utf-8" ?>
   <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="PlanningMaps.vo" assembly="PlanningMaps.vo">
    <class name="PaRegUser, __code" table="PaRegUsers" lazy="false">
        <id name="Id" type="Int64" column="RegUserId">
            <generator class="identity" />
        </id>
        <property name="RegTradeName" column="RegTradeName" type="String" />
        <property name="RegTitle" column="RegTitle" type="String" />
        <property name="RegContactName" column="RegContactName" type="String" />
        <property name="RegPassword" column="RegPassword" type="String" />
        <property name="RegEmail" column="RegEmail" type="String" />
        <property name="RegPhoneNumber" column="RegPhoneNumber" type="String" />
        <bag name="PaRegAddresses" inverse="true" cascade="all-delete-orphan" lazy="false">
            <key column="RegUserID" />
            <one-to-many class="PaRegAddresses, App_Code" />
        </bag>
    </class>
</hibernate-mapping>

儿童:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="PlanningMaps.vo" assembly="PlanningMaps.vo">
    <class name="PaRegAddresses, __code" table="PaRegAddresses" lazy="false">
        <id name="Id" type="Int64" column="RegAddressID">
            <generator class="identity" />
        </id>
        <property name="RegUserId" column="RegUserId" type="Int64" />
        <property name="AddressCity" column="AddressCity" type="String" />
        <property name="AddressCountry" column="AddressCountry" type="String" />
        <property name="AddressLine" column="AddressLine" type="String" />
        <property name="AddressLine1" column="AddressLine1" type="String" />
        <property name="AddressPostcode" column="AddressPostcode" type="String" />
        <many-to-one name="RegUsers" column="RegUserId" not-null="true" class="PaRegUser, App_Code" />
        <property name="IsBillingAddress" column="IsBillingAddress" type="Boolean" />
        <property name="IsShippingAddress" column="IsShippingAddress" type="Boolean" />
    </class>
</hibernate-mapping>

和代码:

public void UpdateRegCustomer( PaRegUser regUser )
    {
    ITransaction tx = null;
    try
        {
        if (!session.IsConnected)
            {
            session.Reconnect();
            }

        tx = session.BeginTransaction();

        if (regUser != null)
            session.Update(regUser);
            tx.Commit();
            }
        session.Disconnect();
        }
    catch (Exception ex)
        {
        tx.Rollback();
        session.Disconnect();
        log.Error("UpdateRegCustomer( PaRegUser regUser ) method", ex);
        throw ex;
        // handle exception
        }
    }

请让我知道这是什么我在这里做错了,我需要做些什么改变才能更新父记录和子记录。
谢谢你的帮助。

1 个答案:

答案 0 :(得分:2)

在孩子中你有:

<property name="RegUserId" column="RegUserId" type="Int64" />

<many-to-one name="RegUsers" column="RegUserId" not-null="true" class="PaRegUser, App_Code" />

两者都引用了RegUsersId列。

我认为您必须将第一个更改为insert =“false”并更新=“false”:

 <property name="RegUserId" column="RegUserId" type="Int64" insert="false" update="false" />

但完全可能是其他东西。

一般来说,虽然您可以删除该属性并执行:

PaRegAddresses.RegUsers.Id