tags:

views:

112

answers:

1

I have a parent child relationship tables set up. In the UI, I show the details of parent and child details. After the user make changes in UI, I need to save it back to the db, but I am getting following error:

NHibernate.ADOException was caught

Message="could not update: [PlanningMaps.vo.PaRegAddresses#2][SQL: UPDATE PaRegAddresses SET RegUserId = ?, AddressCity = ?, AddressCountry = ?, AddressLine = ?, AddressLine1 = ?, AddressPostcode = ?, RegUserId = ?, IsBillingAddress = ?, IsShippingAddress = ? WHERE RegAddressID = ?]"

and innerexception is:

System.Data.SqlClient.SqlException: Column name 'RegUserId' appears more than once in the result column list.

Here is the mapping details: 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>

CHILD:

<?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>

AND THE CODE:

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
        }
    }

Please let me know what it is I am doing wrong here and what change I need to do to be able to update parent and child records.
Thank you for your help.

+2  A: 

In the child you have:

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

and

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

Both refer to the column RegUsersId.

I think you'll have to change the first one to insert="false" and update="false":

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

It could be something else entirely though.

Generally speaking though you could remove the property and do:

PaRegAddresses.RegUsers.Id
eyston
Thanks for your response. I would try this out and post my result here.
Teclioness