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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:01:32+00:00 2026-05-12T07:01:32+00:00

I want map my object model to NHibernate. There is one tricky part in

  • 0

I want map my object model to NHibernate. There is one tricky part in my concept and I don’t know if it is possible to do this in NHibernate.

I want to have a collection of trees. I have two classes (below, only important properties indicated). Component is a node of a tree and ComponentGroup is a collection of trees.

public class Component
{
    public Component Parent { get; set; }
    public IList<Component> SubComponents { get; set; }
    public ComponentGroup Group { get; set; }
}

public class ComponentGroup
{
    public IList<Component> Components { get; set; }
}

Now I want each Component to know which ComponentGroup it belongs to, so I need reference from every Component to ComponentGroup (Group property). But ComponentGroup should have only collection of root nodes (direct children) – Components collection. So this is something like one-to-half mapping 😉 “one” side has reference only to some items from “many” side.

Do you have any ideas how to map something like this using NHibernate?

  • 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-12T07:01:33+00:00Added an answer on May 12, 2026 at 7:01 am

    I’ll give it a shot (generated with FluentNHibernate)

    <class name="Component" table="`Component`" xmlns="urn:nhibernate-mapping-2.2">
    <id name="ComponentId" type="Int32" column="ComponentId">
      <generator class="identity" />
    </id>
    <many-to-one name="Parent" column="ParentId" />
    <bag name="SubComponents">
      <key column="ComponentId" />
      <one-to-many class="NHibernateTests.Component, NHibernateTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
    </bag>
    <many-to-one name="Group" column="GroupId" />
    

    <class name="ComponentGroup" table="`ComponentGroup`" xmlns="urn:nhibernate-mapping-2.2">
    <id name="Id" type="Int32" column="ComponentGroupId">
      <generator class="identity" />
    </id>
    <bag name="Components">
      <key column="ComponentGroupId" />
      <one-to-many class="NHibernateTests.Component, NHibernateTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
    </bag>
    

    EDIT:

    If you want all your Components to know their ComponentGroup then set on all of them the ComponentGroup .

    And in ComponentGroup if you want all the root components only then change the bag to :

    <bag name="Components" where="ParentId is null"> 
    

    so you only get the root components

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

Sidebar

Related Questions

I have this object graph, I want to map: abstract Account (username, password, ...)
I have a database field called abCode , that I want to map to
I want to have a map that has a homogeneous key type but heterogeneous
If I have a std::vector or std::map variable, and I want to see the
So I have a control (a map) on an aspx page. I want to
I want to fill a map with class name and method, a unique identifier
I want something like an std::map , but I only want to see if
I want to texture a sphere with a cube map. So far my research
What I want is lots of nodes which can expand making a mind map.
I want to insert a pair< string, vector<float> > into a map, first it

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.