Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8890625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:32:42+00:00 2026-06-14T22:32:42+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:32:43+00:00Added an answer on June 14, 2026 at 10:32 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I have like this 2 tables class User public int UserId{get;set;} { ....
I am working with some tables where I want the C# class to have
This is the class I am working with: https://gist.github.com/2174233 I have two tables: one
I have three tables which are defined as: class User(Base): __tablename__ = 'users' id
I have two tables, one Company and one Employee: class Company(models.Model): name = models.CharField(max_length=60)
I have next tables Order , Transaction , Payment . Class Order has some
I used JPA and mysql. Each table have entity class.I have four tables. Table1
I have an HTML document where I have two different tables. One is class
I have 2(or more) tables with different ids and class names within each tables
I have a page with several tables on it with the same class name.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.