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 209285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:54:05+00:00 2026-05-11T17:54:05+00:00

I have a class Contact (base class),a class called Customer and a class called

  • 0

I have a class Contact (base class),a class called Customer and a class called Supplier. Customer and supplier class both derive from Contact.

Customer has a 0..n relation with Order. I want to have a Collection property on customer and have it mapped in NHibernate to its corresponding table.

How is this done in NHibernate (version 2.0.1 GA) ?

(ps: using .NET 3.5 SP1, VS2008 SP1)

  • 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-05-11T17:54:05+00:00Added an answer on May 11, 2026 at 5:54 pm

    This is done like this:

    Create your class like this:

    public class Customer  : Contact
    {
       private ISet<Order> _orders = new HashedSet<Order>();
    
       public Collection<Order> Orders
       {
          return new List<Order>(_orders);
       }
    
       // NOrmally I would return a ReadOnlyCollection<T> instead of a Collection<T>
       // since I want to avoid that users add Orders directly to the collection.
       // If your relationship is bi-directional, then you have to set the other
       // end of the association as well, in order to hide this for the programmer
       // I always create add & remove methods (see below)
    
       public void AddOrder( Order o )
       {
          if( o != null && _orders.Contains(o) == false )
          {
             o.Customer = this;
             _orders.Add(o);
          }
       }
    }
    

    in your mapping, you specify this:

    <set name="Orders" table="OrdersTable" access="field.camelcase-underscore" inverse="true">
       <key column="..." />
       <one-to-many class="Order" .. />
    </set>
    

    Since you use inheritance, you should definitly have a look at the different posibilities regarding inheritance-mapping in NHibernate, and choose the strategy that is best suited for your situation:
    inheritance mapping

    Regarding set & bag semantics:
    – when you map a collection as a set, you can be sure that all the entities in the mapped collection, are unique. That is, NHibernate will make sure that, while reconstituting an instance, the collection will not contain duplicates.
    – when you map the collection as a bag, it is possible that your collection will contain the same entity more then once when loading the object from the DB.

    • A Set is a collection of distinct
      objects considered as a whole. A
      valid example of a set (of letters)
      is: { a, b, c, d }. Each letter
      occurs exactly once.
    • A Bag is a generalization of a set. A
      member of a bag can have more than
      one membership while each member of a
      set has only one membership. A valid
      example of a bag is { a, a, a, b, c,
      c, d, …}. The letters a and c
      appear more than once in the Bag.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 198k
  • Answers 198k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer We run a fairly large financial services SaaS on Amazon… May 12, 2026 at 7:28 pm
  • Editorial Team
    Editorial Team added an answer std::cout << "0x" << std::noshowbase << std::hex << std::setw(2) <<… May 12, 2026 at 7:28 pm
  • Editorial Team
    Editorial Team added an answer You are right on the headers you will need to… May 12, 2026 at 7:28 pm

Related Questions

I have a model called Contact which has_one guest like so. class Contact <
In my application at the moment I have (as in so many other applications)
This is a question about tidyness. The project is already working, I'm satisfied with
I am working on a wizard form framework that will allow me easily create

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.