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

  • Home
  • SEARCH
  • 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 577967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:12:38+00:00 2026-05-13T14:12:38+00:00

While trying to create a Bi-directional one-to-one mapping in NHibernate, I found that, I

  • 0

While trying to create a Bi-directional one-to-one mapping in NHibernate, I found that, I am unable to have the Reference of the objects recursively.

For example: suppose I have one-to-one relationships between Person and Address.

then after executing the following code,

class Person
{
    ... ...
    public Address Address { get;set; }
}

class Address
{
    ... ...
    public Person Person {get;set;}
}

Repository<Person> rep = new Repository<Person>();
Person p = rep.Get<Person>(1);

I need to have a non-null value from p.Address.Person. I.e. the same person with an ID of 1.

But the property is returning a null-value.

What should I look for to solve the problem?

My database tables are like this:

Address {ID, Desc}
Person {ID, Name, AddressID}

Person.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping
    xmlns="urn:nhibernate-mapping-2.2"
     default-access="property"
    >
  <class name="NHibernate__BiDirectional__One_To_One.BO.Person, NHibernate__BiDirectional__One_To_One.BO" 
         table="Person">
    <id name="ID">
      <generator class="native" />
    </id>
    <property name="Name"/>

    <many-to-one
        name="Address"
        class="NHibernate__BiDirectional__One_To_One.BO.Address, NHibernate__BiDirectional__One_To_One.BO"
        column="AddressID" 
        cascade="all" 
        unique="true" />

  </class>
</hibernate-mapping>

Address.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping
  xmlns="urn:nhibernate-mapping-2.2"
   default-access="property"
  >
  <class name="NHibernate__BiDirectional__One_To_One.BO.Address, NHibernate__BiDirectional__One_To_One.BO" 
         table="Address">
    <id name="ID" >
      <generator class="native" />
    </id>
    <property name="Desc"/>      
    <one-to-one
        name="Person"
        class="NHibernate__BiDirectional__One_To_One.BO.Person, NHibernate__BiDirectional__One_To_One.BO"
        />
  </class>
</hibernate-mapping>

I am also getting an error:

could not load an entity: [NHibernate__BiDirectional__One_To_One.BO.Person#1][SQ
L: SELECT person0_.ID as ID0_1_, person0_.Name as Name0_1_, address1_.ID as ID1_
0_, address1_.Desc as Desc1_0_, address1_.AddressID as AddressID1_0_ FROM Person
 person0_ left outer join Address address1_ on person0_.ID=address1_.AddressID W
HERE person0_.ID=?]
Incorrect syntax near the keyword 'Desc'.
  • 1 1 Answer
  • 1 View
  • 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-13T14:12:38+00:00Added an answer on May 13, 2026 at 2:12 pm

    There are two varieties of one-to-one association:

    • primary key associations

    • unique foreign key associations

    Primary key associations don’t need an extra table column; if two rows are related by the association then the
    two table rows share the same primary key value. So if you want two objects to be related by a primary key association,
    you must make sure that they are assigned the same identifier value!
    For a primary key association, add the following mappings to Employee and Person, respectively.

    <one-to-one name="Person" class="Person"/>
    <one-to-one name="Employee" class="Employee" constrained="true"/>
    

    Now we must ensure that the primary keys of related rows in the PERSON and EMPLOYEE tables are equal.

    We use a special NHibernate identifier generation strategy called foreign:

    <class name="Person" table="PERSON">
    <id name="Id" column="PERSON_ID">
    <generator class="foreign">
    <param name="property">Employee</param>
    </generator>
    </id>
    ...
    <one-to-one name="Employee"
    class="Employee"
    constrained="true"/>
    </class>
    

    A newly saved instance of Person is then assigned the same primar key value as the Employee instance refered
    with the Employee property of that Person.
    Alternatively, a foreign key with a unique constraint, from Employee to Person, may be expressed as:

    <many-to-one name="Person" class="Person" column="PERSON_ID" unique="true"/>
    

    And this association may be made bidirectional by adding the following to the Person mapping:

    <one-to-one name="Employee" class="Employee" property-ref="Person"/>
    

    Source: Chapter 5. Basic O/R Mapping – 5.1.12. one-to-one

    Have a look at this

    Hibernate Community • View topic – one-to-one with foreign key on child table.

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

Sidebar

Related Questions

I have ran into an interesting issue while trying to create a more usable
While trying to verify to myself, that C# Equals for IEnumerables is a reference
As the title suggest i have (sometimes) a concurrentmodificationexception while trying to create a
I am having a trouble while trying to create an entity with a custom
How can I replace storyboard with XIB? While trying to create dynamically views and
I'm trying to create multiple sheets by iteration in phpexcel: $i=0; while ($i <
Today I was trying to create a application using Hibernate as ORM. So while
While trying to use LINQ to SQL I encountered several problems. I have table
I am trying to create a Custom Media Element , but While trying to
I have been running into OutOfMemory Exceptions while trying to load an 800MB text

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.