使用NHibernate基于外键在子表中插入行 [英] Insert Row in a child table based on Foreign key using NHibernate

查看:103
本文介绍了使用NHibernate基于外键在子表中插入行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景是:我在DB Emp_Details DependentList 中有两个表。 Emp_Details 是主表, DependentList 是子表。我正在尝试使用NHibernate在DependentList中执行插入操作。



我的课程如下:

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>





我是NHibernate的新手,我需要知道如何在家属中插入记录使用employee表中的外键表... 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;
                    }
                }





但这是一个例外:



无法更新:[ModelObjects.Employee#be4d8f67-2b67-41b4-b6f0-a2cde2e0af76] [SQL:UPDATE Emp_Details SET EmpName = ?, EmpDept = ?, Age = ?, DOB = ?, EmpSalary =? ,EmpLocation =?在哪里EmpId =?]



而不是在DependentList中插入它试图更新Emp_Details表...

请帮帮我...



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...

推荐答案

这个问题有没有答案?我们也遇到了这个类似的项目。我们解决它的方法是对外键执行GetById语句,为这些键填充实体对象,然后执行Merge。
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.


this.Session.Save(value);



===>



this.Session.Update(value);
this.Session.Save(value);

===>

this.Session.Update(value);


这篇关于使用NHibernate基于外键在子表中插入行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆