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

Posted by frosty on Stack Overflow See other posts from Stack Overflow or by frosty
Published on 2010-06-02T17:50:41Z Indexed on 2010/06/02 17:54 UTC
Read the original article Hit count: 598

I am getting the following exception.

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

Here are my mapping files.

Product

  <class name="Product" table="Products">
    <id name="Id" type="Int32" column="Id" unsaved-value="0">
      <generator class="identity"/>
    </id> 
    <set name="PriceBreaks" table="PriceBreaks" generic="true" cascade="all" inverse="true" >
      <key column="ProductId" />
      <one-to-many class="EStore.Domain.Model.PriceBreak, EStore.Domain" />
    </set>    

  </class>

Price Breaks

 <class name="PriceBreak" table="PriceBreaks">
    <id name="Id" type="Int32" column="Id" unsaved-value="0">
      <generator class="identity"/>
    </id>
    <property name="ProductId" column="ProductId" type="Int32" not-null="true" />

    <many-to-one name="Product" column="ProductId" not-null="true" cascade="all" class="EStore.Domain.Model.Product, EStore.Domain" />  

  </class>

I get the exception on the following method

[Test]
public void Can_Add_Price_Break()
{

    IPriceBreakRepository repo = new PriceBreakRepository();

    var priceBreak = new PriceBreak();

    priceBreak.ProductId = 19;
    repo.Add(priceBreak);

    Assert.Greater(priceBreak.Id, 0);
}

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about nhibernate-mapping