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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:43:35+00:00 2026-05-20T15:43:35+00:00

Here is my code: Order order = new Order { … }; OrderItem item

  • 0

Here is my code:

Order order = new Order { ... };
OrderItem item = new OrderItem { ... };
order.Items.Add(item);
item.Order = order;

using(var tran = session.BeginTransaction()) {
    session.Save(order);
    // Without this, an exception will be thrown: Unexpected row count: 0; expected: 1
    // session.Save(item);

    tran.Commit();
}

If I uncomment the line session.Save(item), it will insert the order and its item. Otherwise, it throw the exception “Unexpected row count: 0; expected: 1”.

If I mark the many end (Items property) as inverse=”true”, no exception will be thrown, but it inserts only the Order! The order item won’t be inserted to the DB.

If I call session.Save(item) without calling session.Save(order), it inserts both the order and it’s item.

How can I save the order and its order items without calling session.Save(orderItem) but only call session.Save(order) ? Thanks!

Here is my mappings:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Core" assembly="Core">
  <class name="Order" table="[Order]">
    <id name="Id">
      <generator class="assigned" />
    </id>

    <property name="Name" />
    <bag name="Items">
      <key column="CategoryId" />
      <one-to-many class="Core.OrderItem, Core"/>
    </bag>
  </class>

  <class name="OrderItem">
    <id name="Id">
      <generator class="assigned" />
    </id>
    <property name="Title" />
    <many-to-one name="Order" column="OrderId" cascade="save-update" not-null="true" />
  </class>

</hibernate-mapping>
  • 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-20T15:43:35+00:00Added an answer on May 20, 2026 at 3:43 pm

    Because you don’t have any cascade on the Order.Items collection, it will not automatically create the item for you when you save your Order.

    But you do cascade from Item to Order, so when you save Item, it will also update Order.

    When you specified inverse=”true” you told NHibernate not to update the “Item.Order” property automatically, and that you will handle that manually. Therefor the Order is never associated when you add it to the collection hence never shows up there after the commit.

    I think you just need to add:

    <bag name="Items" cascade="all">
    

    and it will create, update, delete items that belongs to orders.

    EDIT: of course cascade goes on the bag, not the one-to-many node.

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

Sidebar

Related Questions

I'm binding ItemsControl to CollectionViewSource. Here is code: this.Trucks = new ObservableCollection<Truck>(); foreach (var
Here's the code: <?php class Order extends Zend_Db_Table_Abstract { protected $_name = 'orders'; protected
Here's the code: <?php class Order extends Zend_Db_Table_Abstract { protected $_name = 'orders'; protected
here is code for regular expression matching #include<iostream> #include<stdio.h> #include<string.h> using namespace std; int
I am attempted to create a new Sales Order in Netsuite using PHPtoolkip_v2010.php and
Here is my simplified code: // Domain models public class Order { public int
I'm trying to add table rows programmatically following the code here /* Find Tablelayout
I have enter code here function ajaxFunction(){ var ajaxRequest; // The variable that makes
Here's my regex code: preg_match_all('/background[-image]*:[\s]*url\([|\']+(.*)[|\']+\)/', $css, $matches, PREG_SET_ORDER); It looks for CSS that looks
The script is on jsfiddle here : CODE What it does at the moment

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.