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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:16:56+00:00 2026-05-12T06:16:56+00:00

I have a problem with nhibernate when I try to insert an object with

  • 0

I have a problem with nhibernate when I try to insert an object with an one-to-one relationship to another object. There is a class called Article

Mapping file

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">

<id name="ID" column="article_id" type="Int32" unsaved-value="0">
    <generator class="native"/>
</id>

<property name="Title" column="title" type="String" not-null="true" length="255" />
<property name="Body" column="body" type="String" length="1073741823" />
<property name="IsEnabled" column="is_enabled" type="Boolean" not-null="true" />

<many-to-one name="ParentArticle" class="..." column="parent_id" cascade="all" />

<bag name="Children" lazy="true" cascade="all-delete-orphan" >
  <key column="parent_id" />
  <one-to-many class="..." />
</bag>

The class itself follows from this mapping file and is omitted for the sake of simplicity.

Then there is another class called SpecialArticle. The mapping for this class is as follows:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">

<id name="ID" column="id" type="Int32" unsaved-value="0">
    <generator class="native"/>
</id>

<property name="Price" column="Price" type="Double" not-null="true" />
<one-to-one name="Article" class="..." fetch="join" cascade="all" />

The ArticleExtension class then is:

    public class SpecialArticle
{
    /// <summary>
    /// Get/Set the ID for this question
    /// </summary>
    public virtual int ID
    {
        get;
        set;
    }

    /// <summary>
    /// Get/Set the expected price for this question or answer
    /// </summary>
    public virtual Double Price
    {
        get;
        set;
    }

    /// <summary>
    /// Get/Set the article that contains the rest of the information for this question
    /// </summary>
    public virtual Article Article
    {
        get;
        set;
    }
}

when i want to insert a special article in the db i do:

// Populate someArticle
// Populate someSpecialArticle
// Following code is wrapped in a transaction
someSpecialArticle.Article = someArticle;
Session.SaveOrUpdate(someArticle);
Session.SaveOrUpdate(someSpecialArticle);

Object someArticle is inserted ok, but the insert statement for object someSpecialArticle is:
INSERT INTO tbl (Price) VALUES (@p0); select SCOPE_IDENTITY()

where is the article_id to specify the parent article???

thx

  • 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-12T06:16:56+00:00Added an answer on May 12, 2026 at 6:16 am

    Proper mapping is:

    <many-to-one name="Article" fetch="join" cascade="all" unique="true" />
    

    When you create one to one mapping in NHibernate one side must be mapped as many-to-one – that will be the side responsible for storing foreign key. Since you have one way relationship here you must map it as many-to-one and use unique constraint to make relationship behave like one-to-one

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

Sidebar

Ask A Question

Stats

  • Questions 144k
  • Answers 144k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use an ItemsControl with customized ItemsPanel, ItemContainerStyle and ItemTemplate: <ItemsControl… May 12, 2026 at 8:35 am
  • Editorial Team
    Editorial Team added an answer if (!checkBox1.Checked && !checkBox2.Checked) { MessageBox.Show("Please select at least one!");… May 12, 2026 at 8:35 am
  • Editorial Team
    Editorial Team added an answer No, I don't agree with your assumptions. 1/ Stored Procedures… May 12, 2026 at 8:35 am

Related Questions

I have a problem. Imagine this data model: [Person] table has: PersonId, Name1 [Tag]
I have a fairly deep object graph (5-6 nodes), and as I traverse portions
NHibernate is driving me 'Nuts'!!! I have the following mapping. Which returns this error
I'm using FluentNHibernate and have done a many-to-many mapping but when I try to
I have a scenario in a system which I've tried to simplify as best

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.