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

The Archive Base Latest Questions

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

I am trying to create a Unidirectional one-to-one relationship using NHibernate. Example: An Order

  • 0

I am trying to create a Unidirectional one-to-one relationship using NHibernate.

Example: An Order is given by a Customer.

Customer{ID, Name, Address}
OrderN{ID, Customer, OrderDate}

Here, OrderN.Customer-field is intended to store Customer.ID as an FK. And this field doesn’t have any unique constraint.

(The OrderN-table is given such a name to avoid SQL keyword conflict.)

The problem is, After executing this c# code, OrderN.Customer-field is storing a null value.

But it was supposed to store the ID of the Customer. I.e. 1.

And if I add <property name="Customer" column="Customer" /> in OrderN.hbm.xml, an exception is thrown:

Could not determine type for: NHibernate__One_To_One__Order_Customer.BO.Customer, NHibernate__One_To_One__Order_Customer.BO, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, for columns: NHibernate.Mapping.Column(Customer)

How to solve this problem?

May be it is not a one-to-one relationship. I am actually trying to understand how is the <one-to-one /> tag used. Can anyone please help me in this regard?

Customer.sql

CREATE TABLE [dbo].[Customer](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) NULL,
    [Address] [varchar](50) NULL,
 CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

Customer.cs

public class Customer
{
    private int _id;
    public virtual int ID
    {
        get { return _id; }
        set { _id = value; }
    }

    private string _name;
    public virtual string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    private string _address;
    public virtual string Address
    {
        get { return _address; }
        set { _address = value; }
    }

}

Customer.hbm.xml

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

<hibernate-mapping
  xmlns="urn:nhibernate-mapping-2.2"
  >
  <class name="NHibernate__One_To_One__Order_Customer.BO.Customer, NHibernate__One_To_One__Order_Customer.BO" table="Customer">
    <id name="ID" >
      <generator class="native" />
    </id>
    <property name="Name" column="Name" />      
    <property name="Address" column="Address" />
  </class>
</hibernate-mapping>

OrderN.sql

CREATE TABLE [dbo].[OrderN](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Customer] [int] NULL,
    [OrderDate] [datetime] NULL,
 CONSTRAINT [PK_Order] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[OrderN]  WITH CHECK ADD  CONSTRAINT [FK_Order_Customer] FOREIGN KEY([Customer])
REFERENCES [dbo].[Customer] ([ID])
GO
ALTER TABLE [dbo].[OrderN] CHECK CONSTRAINT [FK_Order_Customer]

OrderN.cs

public class OrderN
{
    private int _id;
    public virtual int ID
    {
        get { return _id; }
        set { _id = value; }
    }

    private Customer _customer;
    public virtual Customer Customer
    {
        get { return _customer; }
        set { _customer = value; }
    }

    private DateTime _orderDate;
    public virtual DateTime OrderDate
    {
        get { return _orderDate; }
        set { _orderDate = value; }
    }
}

OrderN.hbm.xml

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

<hibernate-mapping 
    xmlns="urn:nhibernate-mapping-2.2" 
    >
  <class name="NHibernate__One_To_One__Order_Customer.BO.OrderN, NHibernate__One_To_One__Order_Customer.BO" table="OrderN">
    <id name="ID">      
      <generator class="native" />
    </id>
    <property name="OrderDate" column="OrderDate"/>


    <one-to-one
        name="Customer"
        class="NHibernate__One_To_One__Order_Customer.BO.Customer, NHibernate__One_To_One__Order_Customer.BO" />


  </class>
</hibernate-mapping>

Main-program

OrderN o = new OrderN();
o.OrderDate = DateTime.Now;
o.Customer = new Repository<Customer>().Get<Customer>(1);

Repository<OrderN> rep = new Repository<OrderN>();
rep.Save(o);
  • 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-13T14:00:47+00:00Added an answer on May 13, 2026 at 2:00 pm

    So a Customer can only ever have one Order? I hope it’s a big one!

    This isn’t a one-to-one relationship, it’s one-to-many. Customer is the one side, and Order is the many (Customer has Orders). Try mapping it like this:

    <?xml version="1.0" encoding="utf-8" ?>
    
    <hibernate-mapping 
        xmlns="urn:nhibernate-mapping-2.2" 
        >
      <class name="NHibernate__One_To_One__Order_Customer.BO.OrderN, NHibernate__One_To_One__Order_Customer.BO" table="OrderN">
        <id name="ID">      
          <generator class="native" />
        </id>
        <property name="OrderDate" column="OrderDate"/>
    
    
        <many-to-one
            name="Customer"
            class="NHibernate__One_To_One__Order_Customer.BO.Customer, NHibernate__One_To_One__Order_Customer.BO"
            column="Customer" />
    
    
      </class>
    </hibernate-mapping>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hi I'm trying create chat using node.js I see example in http://chat.nodejs.org/ I have
I'm trying to create an address book application with one table for People, another
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Ok so I am trying create a login script, here I am using PHP5
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
All, I am trying create a 'to & fro' animation using jquery animate &
I'm trying create a gui using Tkinter that grabs a username and password and
I am trying to generate a unidirectional one-to-many mapping between two JPA entities. In
I am trying create a prototype where one of the features is a dropdown
I'm trying create a regex that verifies an xml entity name is valid (see

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.