Note that there are some explanatory texts on larger screens.

plurals
  1. POForeign key is null when saving
    primarykey
    data
    text
    <p>When trying to save the parent with a many-to-one relationship in the database the child is not updated with the correct father's ID.</p> <p>I have found this questions here that are similar, although they did not work for me:</p> <p><a href="https://stackoverflow.com/questions/4207758/nhibernate-one-to-many-foreign-key-is-null">NHibernate one-to-many foreign key is NULL</a></p> <p><a href="https://stackoverflow.com/questions/8152707/nhibernate-many-to-one-parent-is-always-null-on-insert">nhibernate many-to-one parent is always null on insert</a></p> <p>This is my "father" mapping followed by its class:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;hibernate-mapping assembly="FrancosPoS" namespace="FrancosPoS.DBMapping" xmlns="urn:nhibernate-mapping-2.2"&gt; &lt;class name="order" table="`order`" lazy="true" &gt; &lt;id name="idOrder"&gt; &lt;generator class="identity" /&gt; &lt;/id&gt; &lt;property name="price"&gt; &lt;column name="price" sql-type="decimal(8,4)" not-null="true" /&gt; &lt;/property&gt; &lt;property name="cash"&gt; &lt;column name="cash" sql-type="tinyint(1)" not-null="false" /&gt; &lt;/property&gt; &lt;property name="credit"&gt; &lt;column name="credit" sql-type="tinyint(1)" not-null="false" /&gt; &lt;/property&gt; &lt;property name="obs"&gt; &lt;column name="obs" sql-type="varchar(350)" not-null="false" /&gt; &lt;/property&gt; &lt;bag name="orderPsi" table="ordPsi" cascade="all" inverse="true"&gt; &lt;key column="idOrdPastaI"/&gt; &lt;one-to-many class="ordPsi"/&gt; &lt;/bag&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>Class:</p> <pre><code>namespace FrancosPoS.DBMapping { public partial class order { public order() { } public virtual int idOrder { get; set; } public virtual string price { get; set; } public virtual System.Nullable&lt;int&gt; cash { get; set; } public virtual System.Nullable&lt;int&gt; credit { get; set; } public virtual string obs { get; set; } public virtual IList&lt;ordPsi&gt; orderPsi { get; set; } } } </code></pre> <p>Then, I can have as many orderPsi referring to the same order:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;hibernate-mapping assembly="FrancosPoS" namespace="FrancosPoS.DBMapping" xmlns="urn:nhibernate-mapping-2.2"&gt; &lt;class name="ordPsi" table="ord_psi" lazy="true" &gt; &lt;id name="idOrdPastaI"&gt; &lt;generator class="identity" /&gt; &lt;/id&gt; &lt;many-to-one insert="false" update="false" lazy="false" name="order" class="order"&gt; &lt;column name="idOrder" sql-type="int(11)" not-null="false" /&gt; &lt;/many-to-one&gt; &lt;property name="obs"&gt; &lt;column name="obs" sql-type="varchar(50)" not-null="false" /&gt; &lt;/property&gt; &lt;property name="price"&gt; &lt;column name="price" sql-type="decimal(8,4)" not-null="true" /&gt; &lt;/property&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>Class:</p> <pre><code>public partial class ordPsi { public ordPsi() { } public virtual int idOrdPastaI { get; set; } public virtual order order { get; set; } public virtual string obs { get; set; } public virtual string price { get; set; } } </code></pre> <p>Finally, for saving it:</p> <pre><code>order order = new order(); ordPsi orderPsi = new ordPsi(); order.price = "321"; order.cash = 1; order.credit = 0; orderPsi.order = order; orderPsi.price = "20.00"; order.orderPsi = new List&lt;ordPsi&gt;(); order.orderPsi.Add(orderPsi); //add the child to the father orderDB.setOrder(order); </code></pre> <p>with the database method:</p> <pre><code>public string setOrder(order order) { try { databaseConnection conn = new databaseConnection(); conn.OpenConnection(); conn.Session.SaveOrUpdate(order); conn.Commit(); conn.CloseConnection(); return "Success"; } catch (Exception err) { return "Error: " + err; } } </code></pre> <p>Everything compiles fine and saves to the database because I do not have null constraints, but the ordPsi table is not updated with the orderID.</p> <p>I also tried to save the two objects before committing; leaving the inverse="false" without setting the "orderPsi.order = order", but no success.</p> <p>If this help, I am in most following this two tutorials:</p> <p><a href="http://www.emadashi.com/index.php/2008/08/nhibernate-inverse-attribute/" rel="nofollow noreferrer">NHibernate-inverse-atribute</a></p> <p><a href="http://ajlopez.wordpress.com/2011/05/28/nhibernate-3-part-7-one-to-many-with-inverse/" rel="nofollow noreferrer">one-to-many with inverse</a></p>
    singulars
    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.
 

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