views:

130

answers:

2

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);
}

Following up on Jan reply. I've removed the ProductId from priceBreak map. This works!!

    public int AddPriceBreak(Product product, PriceBreak priceBreak)
    {


        using (ISession session = EStore.Domain.Helpers.NHibernateHelper.OpenSession())
        using (ITransaction transaction = session.BeginTransaction())
        {

            product.AddPriceBreak(priceBreak);
            session.SaveOrUpdate(product);
            transaction.Commit();
        }

        return priceBreak.Id;
    }
A: 

Your usage of Id properties along with the actual references is incorrect.

First, remove this line:

<property name="ProductId" column="ProductId" type="Int32" not-null="true" />

Then, instead of assigning ProductId (you should remove that property completely), use:

priceBreak.Product = session.Load<Product>(19);

(You might need to add the Load method to your repository).

Diego Mijelshon
+1  A: 

Remove the ProductId property from the mapping and from the PriceBreak class. And use the PriceBreaks collection to add PriceBreaks, you don't need the PriceBreakRepository, but only a ProductRepository.

Example:

using (var session = sessionFactory.OpenSession()) 
{
  using (var tx = session.BeginTransaction()) 
  {

    var product = session.Get<Product>(19);
    product.AddPriceBreak(new PriceBreak());

    tx.Commit();
   }
 }

And in the Product:

class Product 
{
   // ...
   public void AddPriceBreak(PriceBreak pb) 
   {
     pb.Product = this;
     PriceBreaks.Add(pb);
   }
 }
Jan Willem B
thanks jan, tried this but it's not passing in the product id. See above
frosty
aah, got tripped up by some constraints. All working thanks
frosty
you're welcome :)
Jan Willem B