Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy one-to-many does not work as expected in NHibernate?
    primarykey
    data
    text
    <p>I have very simple situation and really don't have a clue why this isn't working.</p> <p><strong>This is my schema:</strong></p> <pre><code>**[item]** id (PK) symbol **[item_version]** id (PK) item_id (FK-&gt;ITEM) symbol </code></pre> <p><strong>These are my mappings:</strong></p> <p><em>item.hbm.xml</em></p> <pre><code>&lt;class name="Core.Model.Entities.Item, Core.Model" table="item" lazy="false"&gt; &lt;id name="Id" column="id" type="long"&gt; &lt;generator class="native" /&gt; &lt;/id&gt; &lt;property name="Symbol" column="symbol"/&gt; &lt;bag name="ItemVersions" lazy="false" table="item_version" inverse="true" cascade="save-update"&gt; &lt;key column="item_id" /&gt; &lt;one-to-many class="Core.Model.Entities.ItemVersion, Core.Model"/&gt; &lt;/bag&gt; &lt;/class&gt; </code></pre> <p>*item_version.hbm.xml*</p> <pre><code>&lt;class name="Core.Model.Entities.ItemVersion, Core.Model" table="item_version" lazy="false"&gt; &lt;id name="Id" column="id" type="long"&gt; &lt;generator class="native" /&gt; &lt;/id&gt; &lt;many-to-one name="Item" class="Core.Model.Entities.Item, Core.Model" column="item_id" cascade="save-update"/&gt; &lt;property name="Symbol" column="symbol"/&gt; &lt;/class&gt; </code></pre> <p><strong>These are the classes:</strong></p> <p><em>Item.cs</em></p> <pre><code>public class Item : Entity&lt;IItemDao&gt; { private long id; virtual public long Id { get { return this.id; } set { this.id = value; } } private string symbol; virtual public string Symbol { get { return this.symbol; } set { this.symbol = value; } } private IList&lt;ItemVersion&gt; item_versions = new List&lt;ItemVersion&gt;(); virtual public IList&lt;ItemVersion&gt; ItemVersions { get { return this.item_versions; } set { this.item_versions = value; } } } </code></pre> <p><em>ItemVersion.cs</em></p> <pre><code>public class ItemVersion : Entity&lt;IItemVersionDao&gt; { private long id; virtual public long Id { get { return this.id; } set { this.id = value; } } private Item item = null; virtual public Item Item { get { return this.item; } set { this.item = value; } } private long item_id = 0; virtual public long ItemId { get { return this.item_id; } set { this.item_id = value; } } private string symbol; virtual public string Symbol { get { return this.symbol; } set { this.symbol = value; } } } </code></pre> <p>And this code...</p> <pre><code>Item item = new Item(); item.Name = "test"; ItemVersion v = new ItemVersion(); v.Symbol = "test version"; item.ItemVersions.Add(v); Item.Dao.Save(tmp); </code></pre> <p>... does not work. It inserts an item and throws an exception while inserting item_version row, that item_version.item_id cannot be null. So item_id property isn't automatically set by NHibernate in ItemVersion object. Why cascade="save-update" and inverse="true" does not work in this case?</p> <p>When I set manually, by adding this code:</p> <pre><code>v.Item = item; </code></pre> <p>and than call save, everything works fine, but this is not what I except from NHibernate - calling </p> <pre><code>item.ItemVersions.Add(v); </code></pre> <p>should be sufficient.</p> <p>Am I doing something wrong or this is simply imposible? Thanks in advance for the answers :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload