Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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="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:
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 21-Jun-10 0:30am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

this.Session.Save(value);
 
===>
 
this.Session.Update(value);
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,569
1 Sergey Alexandrovich Kryukov 6,168
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 28 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100