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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:57:02+00:00 2026-05-23T04:57:02+00:00

It might be very simple but I think I am missing something. I have

  • 0

It might be very simple but I think I am missing something. I have a self joined table Units, every unit has a main unit:

enter image description here

So that I could query for something like this:

enter image description here

The table units is mapped to the following class:

public class Units
{
    public virtual int Unit_Id { get; private set; }
    public virtual string Unit { get; set; }
    public virtual decimal Unit_Value { get; set; } 
    public virtual Units Main_Unit { get; set; }
}

With .hbm mapping file as follows:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NewA.Domain"     namespace="NewA.Domain">
   <class name="NewA.Domain.Entities.Units,NewA.Domain" table="Units">
    <id name="Unit_Id" column="Unit_Id" type="Int32" length="4" unsaved-value="0"> 
      <generator class="native">
      </generator>
    </id>
    <property name="Unit" column="Unit" type="string" length="50" not-null="true"/>
    <one-to-one name="Main_Unit" class="NewA.Domain.Entities.Units,NewA.Domain"/>
  </class>
</hibernate-mapping>

When testing these entity with the following code:

Units unit = _UnitsRepository.GetById(2);
string parent_unitname = unit.Main_Unit.Unit;
Assert.AreEqual("pack",parent_unitname);

I got the following excpetion:

Expected values to be equal.

Expected Value : "pack"
Actual Value   : "kg"

The problem is that the Main_Unit property of the Unit entity is referencing itself, so what I am missing here??, and how can I write something like recursive SQL CTE to apply ranks and so on, because I have the same problem with other more complex self joined tables with more complex queries.

  • 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-23T04:57:02+00:00Added an answer on May 23, 2026 at 4:57 am

    You need many-to-one mapping instead one-to-one.
    Try this configuration:

    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
      <class xmlns="urn:nhibernate-mapping-2.2" name="NewA.Domain.Entities.Units.Units, NewA.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Units`">
        <id name="Unit_Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
          <column name="Unit_Id" />
          <generator class="native" />
        </id>
        <property name="Unit" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
          <column name="Unit" />
        </property>
        <property name="Unit_Value" type="System.Decimal, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
          <column name="Unit_Value" />
        </property>
        <many-to-one class="NewA.Domain.Entities.Units.Units, NewA.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="Main_Unit">
          <column name="Main_Unit_id" />
        </many-to-one>
      </class>
    </hibernate-mapping>
    

    I suggest you to try Fluent NHibernate – it can dynamically generate mappings for you.
    Here’s the configuration I used:

    var fluent = Fluently.Configure()
        .Mappings(c => c.AutoMappings.Add(AutoMap.AssemblyOf<Units>()
            .Override<Units>(u => u.Id(uu => uu.Unit_Id).GeneratedBy.Native())))
        .Database(() => SQLiteConfiguration.Standard.UsingFile("test.sqlite3"));
    
    var configuration = fluent.BuildConfiguration();
    
    // Generate database schema
    new SchemaExport(configuration).Create(false, true);
    
    var sessionFactory = configuration.BuildSessionFactory();
    // Now just open session and do whatever you need
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Now this might be a very simple thing but I can't think of the
Ok, I think I might be overlooking something obvious/simple here... but I need to
I know it might be a very simple solution for this, but I am
Forgive what might seem to some to be a very simple question, but I
I know this might be very easy to some,, I have a simple string
This might be something very straight forward and I really think it should work
This might be quite simple but I have spent hours in Google but couldn't
I know this might be a very simple and stupid fix, but I can't
There might be some very simple answer to this, but i am really stuck
This might be a very simple question, but I can't get it working. All

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.