Click here to Skip to main content
15,917,971 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Scenario is: I have two tables in the DB Emp_Details and DependentList. Emp_Details is the primary table and DependentList is the child table. I'm trying to perform insert operation in DependentList using NHibernate.

My classes looks like:
C#
public class Employee
    {
        public virtual Guid ID { get; set; }
        public virtual String Name { get; set; }
        public virtual int Age { get; set; }
        public virtual DateTime DateOfBirth { get; set; }
        public virtual decimal Salary { get; set; }
        public virtual string Location { get; set; }
        public virtual string Department { get; set; }
        public virtual IList<Dependents> UserDependents {get;set;}
   }


C#
public class Dependents
     {
         public virtual Guid ID { get; set; }
         public virtual String Name { get; set; }
         public virtual int Age { get; set; }
         public virtual Employee Employee { get; set; }
         public virtual string Relation { get; set; }
    }


The mapping files are:
Employee:
XML
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="ModelObjects" namespace="ModelObjects">
  <class name="Employee" table="Emp_Details" >
    <id name="ID" column="EmpId" type="Guid">
      <generator class="guid.comb">
      </generator>
    </id>
    <property name="Name" column="EmpName" type="String"></property>
    <property name="Department" column="EmpDept" type="String"></property>
    <property name="Age" column="Age" type="int"></property>
    <property name="DateOfBirth" column="DOB" type="DateTime"></property>
    <property name="Salary" column="EmpSalary" type="Decimal"></property>
    <property name="Location" column="EmpLocation" type="String"></property>
    <bag name="UserDependents" table="DependentList" generic="true" inverse="true" cascade="all">
      <key column="EmpId"></key>
      <one-to-many class="Dependents"/>
    </bag>
  </class>
</hibernate-mapping>



Dependents:
XML
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="ModelObjects" assembly="ModelObjects">
  <class name="Dependents" table="DependentList">
    <id name="ID" column="DependentId" type="Guid">
      <generator class="guid.comb"></generator>
    </id>
    <property name="Name" column="DependentName" type="String"></property>
    <property name="Age" column="DependentAge" type="int"></property>
    <!--<property name="DateOfBirth" column="DependentDOB" type="DateTime"></property>-->
    <property name="Relation" column="DependentRelation" type="String"></property>
    <many-to-one name="Employee" column="EmpId" not-null="false" class="Employee" cascade="all" fetch="join"></many-to-one>
  </class>
</hibernate-mapping>


I'm absolutely new to NHibernate and I need to know how to insert a record into the dependents table using foreign key from employee table... Does the Mapping file specifies Foreign key automatically??

What I am trying to do is:
C#
using (ITransaction trans = this.Session.BeginTransaction())
                {
                    try
                    {
                        this.Session.Save(value);
                        trans.Commit();
                        return true;
                    }
                    catch (HibernateException)
                    {
                        trans.Rollback();
                        return false;
                    }
                }


But this is throwing an exception:

could not update: [ModelObjects.Employee#be4d8f67-2b67-41b4-b6f0-a2cde2e0af76][SQL: UPDATE Emp_Details SET EmpName = ?, EmpDept = ?, Age = ?, DOB = ?, EmpSalary = ?, EmpLocation = ? WHERE EmpId = ?]

instead of inserting in DependentList it is trying to update Emp_Details table...
Please help me...
Posted

Has there been an answer to this issue? We are running into this similar item also. The way we are resolving it is to do a GetById statement on the foreign keys, populate the entity object for those keys, then execute a Merge.
 
Share this answer
 
this.Session.Save(value);

===>

this.Session.Update(value);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900