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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:07:36+00:00 2026-06-11T19:07:36+00:00

When trying to save the parent with a many-to-one relationship in the database the

  • 0

When trying to save the parent with a many-to-one relationship in the database the child is not updated with the correct father’s ID.

I have found this questions here that are similar, although they did not work for me:

NHibernate one-to-many foreign key is NULL

nhibernate many-to-one parent is always null on insert

This is my “father” mapping followed by its class:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="FrancosPoS" namespace="FrancosPoS.DBMapping" xmlns="urn:nhibernate-mapping-2.2">
  <class name="order" table="`order`" lazy="true" >
    <id name="idOrder">
      <generator class="identity" />
    </id>
    <property name="price">
      <column name="price" sql-type="decimal(8,4)" not-null="true" />
    </property>
    <property name="cash">
      <column name="cash" sql-type="tinyint(1)" not-null="false" />
    </property>
    <property name="credit">
      <column name="credit" sql-type="tinyint(1)" not-null="false" />
    </property>
    <property name="obs">
      <column name="obs" sql-type="varchar(350)" not-null="false" />
    </property>
    <bag name="orderPsi" table="ordPsi" cascade="all" inverse="true">
      <key column="idOrdPastaI"/>
      <one-to-many class="ordPsi"/>
    </bag>
  </class>
</hibernate-mapping>

Class:

namespace FrancosPoS.DBMapping
{    
    public partial class order
    {
        public order() { }
        public virtual int idOrder { get; set; }
        public virtual string price { get; set; }
        public virtual System.Nullable<int> cash { get; set; }
        public virtual System.Nullable<int> credit { get; set; }
        public virtual string obs { get; set; }
        public virtual IList<ordPsi> orderPsi { get; set; }
    }
}

Then, I can have as many orderPsi referring to the same order:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="FrancosPoS" namespace="FrancosPoS.DBMapping" xmlns="urn:nhibernate-mapping-2.2">
  <class name="ordPsi" table="ord_psi" lazy="true" >
    <id name="idOrdPastaI">
      <generator class="identity" />
    </id>
    <many-to-one insert="false" update="false" lazy="false" name="order" class="order">
      <column name="idOrder" sql-type="int(11)" not-null="false" />
    </many-to-one>
    <property name="obs">
      <column name="obs" sql-type="varchar(50)" not-null="false" />
    </property>
    <property name="price">
      <column name="price" sql-type="decimal(8,4)" not-null="true" />
    </property>
  </class>
</hibernate-mapping>

Class:

public partial class ordPsi
{
    public ordPsi() { }
    public virtual int idOrdPastaI { get; set; }
    public virtual order order { get; set; }
    public virtual string obs { get; set; }
    public virtual string price { get; set; }
}

Finally, for saving it:

order order = new order();
ordPsi orderPsi = new ordPsi();

order.price = "321";
order.cash = 1;
order.credit = 0;

orderPsi.order = order;
orderPsi.price = "20.00";
order.orderPsi = new List<ordPsi>();
order.orderPsi.Add(orderPsi); //add the child to the father
orderDB.setOrder(order);

with the database method:

public string setOrder(order order)
{
    try
    {
        databaseConnection conn = new databaseConnection();
        conn.OpenConnection();

        conn.Session.SaveOrUpdate(order);

        conn.Commit();
        conn.CloseConnection();

        return "Success";
    }
    catch (Exception err)
    {
        return "Error: " + err;
    }
}

Everything compiles fine and saves to the database because I do not have null constraints, but the ordPsi table is not updated with the orderID.

I also tried to save the two objects before committing; leaving the inverse=”false” without setting the “orderPsi.order = order”, but no success.

If this help, I am in most following this two tutorials:

NHibernate-inverse-atribute

one-to-many with inverse

  • 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-06-11T19:07:37+00:00Added an answer on June 11, 2026 at 7:07 pm

    Remove update=”false” insert=”false” in your OrdPsi mapping – they explicitly tell NHibernate not to set the value on the column when updating or inserting.. so NHibernate will basically exclude it when it inserts it, hence the null value.

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

Sidebar

Related Questions

I have a bidirectional one-to-many relationship. I'm trying to persist it like in this
I'm using Entity Framework 4 and have a one-to-many relationship between a parent and
I'm trying to save (insert) parent object with a collection of child objects, all
In a situation where you have a parent class which has one child class,
I'm trying to figure what's the correct way to map the following parent child
Typical problem - I am trying to save new parent object with nested new
When trying to save the database using boost serialization, I encounter the segfault that
I'm trying to add has_many_through relationship through a form nested in a parent model.
I am trying to save a file using GetSaveFileName and want to have a
I have a parent entity (Treatment) with a collection of child entities (Segments). I

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.