Reputation: 316
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.
I have found this questions here that are similar, although they did not work for me:
NHibernate one-to-many foreign key is NULL
nhibernate many-to-one parent is always null on insert
This is my "father" mapping followed by its class:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="FrancosPoS" namespace="FrancosPoS.DBMapping" xmlns="urn:nhibernate-mapping-2.2">
<class name="order" table="`order`" lazy="true" >
<id name="idOrder">
<generator class="identity" />
</id>
<property name="price">
<column name="price" sql-type="decimal(8,4)" not-null="true" />
</property>
<property name="cash">
<column name="cash" sql-type="tinyint(1)" not-null="false" />
</property>
<property name="credit">
<column name="credit" sql-type="tinyint(1)" not-null="false" />
</property>
<property name="obs">
<column name="obs" sql-type="varchar(350)" not-null="false" />
</property>
<bag name="orderPsi" table="ordPsi" cascade="all" inverse="true">
<key column="idOrdPastaI"/>
<one-to-many class="ordPsi"/>
</bag>
</class>
</hibernate-mapping>
Class:
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<int> cash { get; set; }
public virtual System.Nullable<int> credit { get; set; }
public virtual string obs { get; set; }
public virtual IList<ordPsi> orderPsi { get; set; }
}
}
Then, I can have as many orderPsi referring to the same order:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="FrancosPoS" namespace="FrancosPoS.DBMapping" xmlns="urn:nhibernate-mapping-2.2">
<class name="ordPsi" table="ord_psi" lazy="true" >
<id name="idOrdPastaI">
<generator class="identity" />
</id>
<many-to-one insert="false" update="false" lazy="false" name="order" class="order">
<column name="idOrder" sql-type="int(11)" not-null="false" />
</many-to-one>
<property name="obs">
<column name="obs" sql-type="varchar(50)" not-null="false" />
</property>
<property name="price">
<column name="price" sql-type="decimal(8,4)" not-null="true" />
</property>
</class>
</hibernate-mapping>
Class:
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; }
}
Finally, for saving it:
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<ordPsi>();
order.orderPsi.Add(orderPsi); //add the child to the father
orderDB.setOrder(order);
with the database method:
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;
}
}
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.
I also tried to save the two objects before committing; leaving the inverse="false" without setting the "orderPsi.order = order", but no success.
If this help, I am in most following this two tutorials:
Upvotes: 0
Views: 865
Reputation: 5679
Remove update="false" insert="false" in your OrdPsi mapping - they explicitly tell NHibernate not to set the value on the column when updating or inserting.. so NHibernate will basically exclude it when it inserts it, hence the null value.
Upvotes: 1