views:

79

answers:

1

I have a Client Table with One to Many relationship with Contact Persons. I've mapped the two classes for NHibernate as

In Client Class

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

In ContactPerson Class

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

I am using Windsor Castle for NHibernate. Here is the Code for Saving the Client.

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);

I've set insert="false" update="false" for ClientId (Foreign key in ContactPerson) to stop NHibernate from generating Extra Column in Insert Statement.

The Problem is the Model Gets Updated correctly from FormCollection. If I add a New Client, Contact Persons also get persisted when I update the Client and cascading works fine. But, If I edit existing client, it loads the existing contact persons, updates the data from FormCollection. But Updating the Client does not cascade and execute the Update of Contact Persons. Thus New Contact persons never get persisted and even existing contact person's data changes are not saved.

If Insert is ok and getting cascaded to Contactpersons when I update the client. What must be the Problem with Update?

I am also setting the Client Property of Contact Person objects in collection in setter of the collection which is getting executed.

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

I have tried everything in the Posts I came across on the Web. Now getting frustrated with this. Can anybody help?

A: 

I'm not sure if that's the issue but we had a similar problem and we found out that whenever we were using a generic IList collection,cascade mechanism wasn't working. So we used non generic collections

Beatles1692
Why the downvote? Seems like a reasonable suggestion.
Tom Bushell
:) same question here !
Beatles1692
I am not using Generic List. Check the Code in my post it uses ISet<ContactPerson>
Protean
Well ISet<ContactPerson> seems generic to me :)
Beatles1692
OH! I didn't mean that it's a generic list issue but I suspect it's rather a generic issue
Beatles1692