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've been doing things like this: import Tkinter class C(object): def __init__(self): self.root
Recently, I often encounter errors like this: Debugger entered--Lisp error: (wrong-type-argument number-or-marker-p nil) I
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 wrote something like this: public void doSomething(boolean b1, boolean b2){ while(true){ if(b1){
Recently one of our app servers went down, when it was rebooted the Python
Recently I've found how to add a link to an item in Visual Studio
Recently, I was writing a class in which I discovered that I could reduce
recently, while working on a db2 -> oracle migration project, we came across this
Recently I'm much excited about linux. I would like to learn how to write

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.