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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:04:16+00:00 2026-05-31T12:04:16+00:00

Recently I’ve found an example on NHibernate’s one-to-many relationship. The mapping goes like this:

  • 0

Recently I’ve found an example on NHibernate’s one-to-many relationship. The mapping goes like this:

<class name="Company" table="Company">
    <id name="Id" type="Int32" unsaved-value="0">
      <generator class="identity"/>
    </id>
    <version name="Version"/>
    <property name="Name"/>
    <set name="Customers" inverse="true" lazy="true" >
      <key column="Id"/>
      <one-to-many class="Customer"/>
    </set> 
</class>

<class name="Customer" table="Customer">
    <id name="Id" type="Int32" unsaved-value="0">
      <generator class="identity"/>
    </id>
    <version name="Version"/>
    <property name="DateOfBirth"/>
    <property name="FirstName"/>
    <property name="Surname"/>
    <many-to-one name="Company" column="CompanyId" cascade="all-delete-orphan"/>
</class>

And this are the classes:

public class Company
{
    private ISet<Customer> customers = new HashedSet<Customer>();
    public int Id { get; set; } 
    public string Name { get; set; }
    public int Version { get; set; }
    public ISet<Customer> Customers
    {
        get { return customers; }
        set { customers = value; }
    }
}

public class Customer
{
    public Company Company { get; set; }
    public DateTime? DateOfBirth { get; set; }
    public string FirstName {get;set;}
    public int Id { get; set; }
    public string Surname {get;set;}
    public int Version { get; set; }
}

What I don’t like about this code is the inclusion of the parent inside the child, the whole Company object inside the Customer one. Won’t this be a circular reference. I mean, when this graph loads won’t I be able to do something like currentCustomer.Company.Customers.First().Company.Customers.Last() … and so on? Or it doesn’t work like that?

Assuming the underlying table Customer had only a string column CompanyName, how could I map that? How would be the mapping if instead of a public Company Company I’d have a public string CompanyName in the Customer class?

  • 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-31T12:04:17+00:00Added an answer on May 31, 2026 at 12:04 pm

    yes you can write:

    currentCustomer.Company.Customers.First().Company.Customers.Last() 
    

    as many time you want inside an open session, this will issue a query for loading the company, then (probably one) for fetching the Customers collection, and then all will happen in memory. There is nothing so strange about it, apart the fact that probably is not so useful. If you just have a company name, yes you can map it as a simple property of type string.

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

Sidebar

Related Questions

Recently I bought a new android tablet (a no-name Chinese tablet), and I'd like
Recently I have been told that static class/methods are evil. Take for example my
Recently, I came across this one stone kills multiple birds framework - http://phonegap.com/ .
Recently I found about this tool easy_install that help me to easy install additional
Recently I had to develop a SharePoint workflow, and I found the experience quite
Recently I ran into this error in my web application: java.lang.OutOfMemoryError: PermGen space It's
Recently two users of our software from the same company started experiencing random closures
Recently, we discovered odd behavior in some old code. This code has worked for
Recently I've been doing quite the project mostly working with the DateTime class. Now,..
Recently I've read this performance guide Let's make the web faster and was puzzled

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.