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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:57:20+00:00 2026-05-11T11:57:20+00:00

I am trying to persist an object with a collection of child objects. I

  • 0

I am trying to persist an object with a collection of child objects. I can’t persist the children first as there is a FK relationship. I could save the parent first and then add the children on to it, but this would introduce more work. Basically I’m just trying to save a fully populated object in one step and not break it into parts. Is there something wrong with my mapping (sorry it looks so ugly) or is it my methods?

Parent:

<?xml version='1.0' encoding='utf-8' ?> <hibernate-mapping xmlns='urn:nhibernate-mapping-2.2'>   <class name='NetworkOrderManagement.Core.Order, NetworkOrderManagement.Core' table='NETORDMGMT.ORDERHEADER' lazy='false' >     <id name='OrderId' column='ORDERID' type='int'>       <generator class='seqhilo'>         <param name='sequence'>ORDERID_SEQ</param>       </generator>     </id>     <property name='TransmissionDate' column='TRANSMISSIONDATE' type='DateTime'/>     <property name='StoreNumber' column='STORENUMBER' type='Int16'/>     <property name='Department' column='DEPARTMENT' type='Int16'/>     <property name='OrderType' column='ORDERTYPE' type='Int16'/>     <property name='OrderSequence' column='ORDERSEQUENCE' type='Int16'/>     <property name='ExtractTime' column='EXTRACTTIME' type='DateTime'/>     <property name='Status' column='STATUS' type='Int16'/>     <property name='ReceivedTime' column='RECEIVEDTIME' type='DateTime'/>     <bag name='OrderDetail' table='NETORDMGMT.ORDERDETAIL'           lazy='false' cascade='all' inverse='true'>       <key column='ORDERID' on-delete='cascade'/>       <one-to-many class='NetworkOrderManagement.Core.OrderDetail, NetworkOrderManagement.Core'  />     </bag>   </class> </hibernate-mapping> 

Child:

<?xml version='1.0' encoding='utf-8' ?> <hibernate-mapping xmlns='urn:nhibernate-mapping-2.2'>   <class name='NetworkOrderManagement.Core.OrderDetail, NetworkOrderManagement.Core' table='NETORDMGMT.ORDERDETAIL' lazy='false'>     <id name='OrderDetailId' column='ORDERDETAILID' type='int'>       <generator class='seqhilo'>         <param name='sequence'>'ORDERDTLID_SEQ'</param>       </generator>     </id> <many-to-one name='Order' class='NetworkOrderManagement.Core.Order, NetworkOrderManagement.Core'                  column='OrderId' lazy='false' not-null='true' />     <property name='ItemNumber' column='ITEMNUMBER' type='Int32'/>     <property name='OrderQuantity' column='ORDERQUANTITY' type='Int32'/>     <property name='ErrorCode' column='ERRORCODE' type='Int32'/>   </class> </hibernate-mapping> 

Here’s my exception:

Test method NetworkOrderManagement.Tests.DataAccess.QuickTests.QuickTest threw exception: Distribution.Exceptions.DataAccessException: NHibernate Exception ---> NHibernate.PropertyValueException: not-null property references a null or transient valueNetworkOrderManagement.Core.OrderDetail.Order. 

I get this when my test below tries to add an orderdetail to the order while it is still transient:

    [TestMethod]     public void QuickTest()     {         myOrderRepository = NetworkOrderManagement.Data.RepositoryFactory.Instance.GetOrderRepository();         myOrderDetailRepository = NetworkOrderManagement.Data.RepositoryFactory.Instance.GetOrderDetailRepository();         myOrder = new Order { StoreNumber = RandGen.LittleRand(), Department = RandGen.LittleRand(), TransmissionDate = DateTime.MinValue, ExtractTime = DateTime.MinValue, ReceivedTime = DateTime.MinValue };         myOrder = myOrderRepository.Save(myOrder);          myOrderDetail1 = new OrderDetail {OrderId = myOrder.OrderId, ItemNumber = RandGen.BigRand(), OrderQuantity = RandGen.LittleRand() };         myOrderDetail2 = new OrderDetail {OrderId = myOrder.OrderId, ItemNumber = RandGen.BigRand(), OrderQuantity = RandGen.LittleRand() };         myOrderDetail1 = myOrderDetailRepository.Save(myOrderDetail1);         myOrderDetail2 = myOrderDetailRepository.Save(myOrderDetail2);         myOrder.OrderDetail.Add(myOrderDetail1);         myOrder.OrderDetail.Add(myOrderDetail2);          myOrderRepository.CommitChanges();          myOrderDetailRepository.Delete(myOrderDetail2);         myOrderRepository.CommitChanges();         myOrderRepository.Delete(myOrder);         myOrderRepository.CommitChanges();     } 
  • 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. 2026-05-11T11:57:20+00:00Added an answer on May 11, 2026 at 11:57 am

    Specify cascading on the collection, and let NHibernate figure it out for you

    http://ayende.com/Blog/archive/2006/12/02/NHibernateCascadesTheDifferentBetweenAllAlldeleteorphansAndSaveupdate.aspx

    http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/example-parentchild.html

    Okay, I’ve seen that you’ve done that. 🙂 What you haven’t done, is specify the back-reference. I mean: you add an item to your collection, but this added item has a property to its owner, which you haven’t set:

    Order o = new Order();  OrderDetail detail = new OrderDetail ();  detail.Order = o; o.OrderLines.Add (detail); 

    What would be even better (imho) is this (simplified) :

    public class Order {     private IList<OrderDetail> _details = new List<OrderDetail>();      public ReadOnlyCollection<OrderDetail> Details     {        return new List(_details).AsReadOnly();     }      public void AddOrderLine( OrderDetail d )     {         d.Order = this;         _details.Add (d);     }      public void RemoveOrderLine( OrderDetail d )     {         _details.Remove (d);     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to persist an object into the database. This operation should touch
I'm trying to persist my object but when I look in the dev console,
I am trying to persist a Template object in the app's database. It's not
i am trying to persist two entities(with one to many relationship) using jpa but
I'm trying to do a cascading save on a large object graph using JPA.
I am trying to persist Java objects to the GAE datastore. I am not
I'm trying to persist an object into the database using Cayenne Entity Manager. And
I am trying to use NHibernate to persist objects using Guids for their Ids
I am getting below error whilst trying to persist an object that has a
I'm using JPA2 and Hibernate implementation. I'm trying to persist a User object, which

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.