Creating an NHibernate Object and Initializing a Set

I have a table called Product, and I have a StorageHistory table.

The Product now contains a reference to StorageHistory in its mappings

<set name="StorageHistories" lazy="false">
  <key column="ProductId" />
  <one-to-many class="StorageHistory" />
</set>

And it works, when I retrieve an object from ORM, I get an empty ISet.

What gives me a headache is how to build an object in the first place. When I do the following:

var product = new Product();
session.Save(product);

the product.StorageHistories property is NULL and I get a NullReferenceException. So, how do I add items to this collection, or should I go to add StorageHistory items directly to the database?

+3
source share
2 answers

I always do the following in the ctor of the parent object:

history = new HashedSet ();

Save(). ()/Get() etc usecase NHibernate, .

+6

?

private ISet _StorageHistories;
public virtual ISet StorageHistories {
     protected set { _StorageHistories = value;}
     get { if (_StorageHistories == null) _StorageHistories = new HashSet();
           return _StorageHistories;
     }
}

, , .

0

Source: https://habr.com/ru/post/1699039/


All Articles