Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate WARNING and the data is not saved

Tags:

nhibernate

While I am saving by calling SaveOrUpdate(), I got this warning and the data is not saving in the database after calling Transaction.Commit().

NHibernate.Engine.ForeignKeys - Unable to determine if [project name] with assigned identifier [primarykey] is transient or detached; querying the database. Use explicit Save() or Update() in session to prevent this.

I am inserting a new object. Google search tell me to call Save() instead of SaveOrUpdate(): means Save() is only for inserting.

I search in Google and do not see much about this. Could anyone give me suggestion for this problem or this warning?

Edit: Here is the simulated sample mapping files -

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly=""
                   namespace="">
    <class name="Customer" table="[dbo].[Customer]" optimistic-lock="none" >
        <id name="CustomerId" column="CustomerId" >
            <generator class="assigned"/>
        </id>
        <property name="Name" column="Name" /> 
        <property name="Age" column="Age" /> 
        <set name="CustomerDetails" cascade="none" inverse="true" fetch="select">
            <key>
                <column name="CustomerId"/>
            </key>
            <one-to-many class="CustomerDetail"/>
        </set> 
        <many-to-one name="MGender" fetch="select" cascade="none">
            <column name="GenderCode"/>
        </many-to-one> 
    </class>
</hibernate-mapping>

<class name="CustomerDetails" table="[dbo].[CustomerDetail]" optimistic-lock="none" >
    <id name="CustomerDetailId" column="CustomerDetailId" >
        <generator class="assigned"/>
    </id>
    <property name="Detail1" column="Detail1" /> 
    <many-to-one name="Customer" fetch="select" cascade="none">
        <column name="CustomerId"/>
    </many-to-one> 
</class>

<class name="MGender" table="[dbo].[MGender]" optimistic-lock="none" >
    <id name="GenderCode" column="GenderCode" >
        <generator class="assigned"/>
    </id>
    <property name="Description" column="Description" /> 
    <set name="Customers" cascade="none" inverse="true" fetch="select">
        <key>
            <column name="GenderCode"/>
        </key>
        <one-to-many class="Customer"/>
    </set> 
</class>

like image 216
TMS Avatar asked Dec 14 '25 09:12

TMS


1 Answers

You're using an assigned identifier so you need to set the unsaved-value attribute so that NHibernate can determine if an entity should be inserted or updated. Or you can explicitly call Save for new entities.

    <id name="CustomerId" column="CustomerId" unsaved-value="???" >
        <generator class="assigned"/>
    </id>
like image 75
Jamie Ide Avatar answered Dec 16 '25 22:12

Jamie Ide



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!