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:
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;}
}
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:
<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:
<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="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:
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...