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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:43:54+00:00 2026-06-04T21:43:54+00:00

Clearly I am searching on the wrong keywords. I have objects Vendor and Invoice

  • 0

Clearly I am searching on the wrong keywords.

I have objects Vendor and Invoice in an NHibernate project like so:

public class Vendor
{
    public string theName { get; set; }
}

public class Invoice
{
    public Vendor theVendor { get; set; }
    public decimal Amount { get; set; }
}

I have a UnitOfWork style interface with NH VendorRepository and InvoiceRepository repositories… these seem to be working.

I can create a Vendor no sweat, sorta like this:

public void CreateVendor(string name)
{
    using (var u = new UnitOfWork())
    {
        var v = new Vendor();
        v.theName = name;
        u.Vendors.Add(v);
        u.Save();
    }
}

Creating the Invoice object, however, requires a reference to Vendor. I thought this would be as simple as:

public void CreateInvoice(decimal theAmount, string vendorName)
{
    using (var u = new UnitOfWork())
    {
        var i = new Invoice();
        i.Amount = theAmount;
        var v = u.Vendors.GetByName(vendorName);
        i.theVendor = v;
        u.Invoices.Add(i);
        u.Save();
    }
}

However, this does not appear to be the case. Do I need to do some special NHibernate voodoo for this? I’d prefer to keep the NHibernate behind the UnitOfWork interface, but I’d like to cleanly associate the objects with minimal code or confusion.

UPDATE Per request:
Mapping file (extra stuff removed) for Vendor:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="DataModel"
                   namespace="DataModel">
    <class name="Vendor" table="Vendors">
        <id name="APVendorId">
            <generator class="guid" />
        </id>
        <property name="theName" index="ixVendorName" length="100" not-null="true" column="VendorName" />
    </class>
</hibernate-mapping>

and for Invoice:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                    assembly="C3.DataModel"
                    namespace="C3.DataModel">
    <class name="Invoice" table="Invoices">
        <id name="InvoiceId">
            <generator class="guid" />
        </id>
        <one-to-one name="Vendor" class="Vendor" constrained="true" cascade="none" fetch="join" />
        <property name="theAmount" column="InvoiceAmount" not-null="true" />
    </class>
</hibernate-mapping>

The repository code resembles this:

///<summary>
///Auto-generated NHibernate repository for the domain POCO object <strong>APInvoice</strong>
///</summary>
public partial class NHInvoiceRepository : IInvoiceRepository
{
    internal ISession _Session;
    internal ITransaction _Transaction;
    private bool _IndependentSession;

    ///<summary>
    ///Adds a Invoice object to the NHibernate repository.
    ///</summary>
    public void Add(Invoice invoice)
    {
        _Session.Save(invoice);
    }

    ///<summary>
    ///Instantiates the repository in standalone (no associated UnitOfWork) mode.  This should usually be avoided.
    ///</summary>
    internal NHInvoiceRepository()
    {
        _Session = NHibernateHelper.OpenSession();
        _Transaction = _Session.BeginTransaction();
        _IndependentSession = true;
    }

    ///<summary>
    ///Instantiates the repository as a part of a UnitOfWork pattern.
    ///</summary>
    public NHInvoiceRepository(ISession session, ITransaction transaction)
    {
        _Session = session;
        _Transaction = transaction;
        _IndependentSession = false;
    }

    ///<summary>
    ///Instantiates the repository as a part of a UnitOfWork pattern.
    ///</summary>
    public NHInvoiceRepository(ISession session)
    {
        _Session = session;
        _Transaction = _Session.BeginTransaction();
        _IndependentSession = false;
    }

    ///<summary>
    ///Implements the IDisposable interface.
    ///</summary>
    public void Dispose()
    {
        _Transaction.Dispose();
        _Session.Dispose();
        return;
    }

    ///<summary>
    ///Commits the changes in the repository in standalone (no associated UnitOfWork) mode.
    ///</summary>
    public void Save()
    {
        if (_IndependentSession)
        {
            _Transaction.Commit();
            _Transaction = _Session.BeginTransaction();
        }
    }

}

The UnitOfWork code looks like this:

///<summary>
///UnitOfWork Interface.  The primary data interface between the data model and the persistence layer.
///</summary>
public partial class NHUnitOfWork : IUnitOfWork
{
    private ISession _Session;
    private ITransaction _Transaction;
    public IVendorRepository Vendors { get; private set; }
    public IInvoiceRepository Invoices { get; private set; }

    public void Save()
    {
        Vendors.Save();
        Invoices.Save();
        _Transaction.Commit();
    }

    public void Dispose()
    {
        Vendors.Dispose();
        Invoices.Dispose();
        _Transaction.Dispose();
        _Session.Dispose();
    }

    public NHUnitOfWork()
    {
        _Session = NHibernateHelper.OpenSession();
        _Transaction = _Session.BeginTransaction();
        Vendors = new NHVendorRepository(_Session);
        Invoices = new NHInvoiceRepository(_Session);
    }
}

The error message I am getting is:

NHibernate.PropertyValueException: not-null property references a null or transient value 

I did try changing the one to one to a many-to-one in the mappings, with no different results.

UPDATE on the second recompile, (and rebuilding the db schema) this change worked.

  • 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-04T21:43:54+00:00Added an answer on June 4, 2026 at 9:43 pm

    I always have troubles with <one-to-one> mappings. I suggest to move to <many-to-one unique="true">

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

Sidebar

Related Questions

This looks like it should work to me, but clearly I've done something wrong.
I'd like to have all references to my site redirect to blah.mysite.com . I
I am searching for some information. I have seen in many programmes following files
Good day, I have been searching for a way to format output using writeline/write
I have a class that implements IDisposable because it has a private member field
I have been searching for the perfect web development IDE for some time now.
After searching, I have failed to find out an easily understandable explaination as to
After a lot of searching I wasn't able to find anything that clearly describes
We have a project using Entity Framework 4.1 Code First for data storage. It
Clearly it's not quite true that SQL floats are the same as C# Doubles

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.