Search code examples
c#nhibernate-mappingone-to-many

Save data into multiple tables


I have 2 db tables and class structure working with nhibernate as follows.

DB

enter image description here

C# poco classes are as follow.

Customer Class

public class Customer
{
    public virtual int CustomerID { get; set; }
    public virtual string CustomerName { get; set; }
    public virtual string Title { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual string Mobile { get; set; }
    public virtual string Phone { get; set; }
    public virtual string Email { get; set; }

    public virtual Address BillingAddress { get; set; }
    public virtual Address ShippingAddress { get; set; }
}

Address Class

public class Address
{
    public virtual int AddressID { get; set; }
    public virtual string AddressLine { get; set; }
    public virtual string City { get; set; }
    public virtual string State { get; set; }
    public virtual int ZIP { get; set; }
    public virtual string Fax { get; set; }
    public virtual string Country { get; set; }
}

Mapping Files

customer.hbm.xml

  <class name="Customer" table="Customers">
    <id name="CustomerID">
      <generator class="native" />
    </id>
    <property name="CustomerName" length="100" />
    <property name="Title" length="10" />
    <property name="FirstName" length="60" />
    <property name="LastName" length="60" />
    <property name="Mobile" length="15" />
    <property name="Phone" length="15" />
    <property name="Email" length="100" />
    <many-to-one name="BillingAddress" class="Address" />
    <many-to-one name="ShippingAddress" class="Address" />
  </class>

address.hbm.xml

  <class name="Address" table="Addresses">
    <id name="AddressID">
      <generator class="native" />
    </id>
    <property name="AddressLine" length="255" />
    <property name="City" length="30" />
    <property name="State" length="30" />
    <property name="ZIP" />
    <property name="Fax" length="15" />
    <property name="country" length="50" />
  </class>

I am creating the database tables using nhibernate session. Tables are creating fine. with the above structure, If I want to save a customer with 2 address types specified, how can I save that 1 customer to customers table and 2 address to addresses table? See the following example code.

Address b_address = new Address();
Address s_address = new Address();
Customer customer = new Customer();

b_address.addressLine = "No.23, New Road";
b_address.City = "Newyork";
// more data

s_address.addressLine = "No.54, Old Road";
// more data

customer.CustomerName = "David";
// more customer data
customer.BillingAddress = b_address;
customer.ShippingAddress = s_address;

//saving the session.
session.save(customer)

The above code only insert the data into customers table. How would I make this insert data to both customers and addresses table?


Solution

  • We need to put casacde setting in play. Just change the many-to-one mapping this way:

    <many-to-one name="BillingAddress" class="Address" cascade="all"  />
    <many-to-one name="ShippingAddress" class="Address" cascade="all"  />
    

    This will allow NHibernate to persist new Addresses and update existing, while only calling Customer instance to be saved/updated.