【问题标题】:NHibernate - one-to-one MappingNHibernate - 一对一映射
【发布时间】:2011-06-14 02:38:21
【问题描述】:

我在 Person 类和 Employee 之间建立了一对一的关系。我希望 INSERT 从 Person 级联到 Employee。但是,这不会发生。我已经在一对一的关系元素上尝试了 cascade='all' 和 cascade='save-update' ,但它没有用。我还将我的整个源代码上传到:http://bit.ly/gnkxBr (3.52 MB)

my对象的结构如下:

public class Person
{
    public virtual Employee Employee { get; set; }
    public virtual int Age { get; set; }
    public virtual string Forename { get; set; }
    public virtual string Surname { get; set; }
    public virtual int PersonID { get; set; }
}

public class Employee
{
    public virtual int PersonID { get; set; }
    public virtual string PayRollNo { get; set; }
    public virtual int Holidays { get; set; }
    public virtual Person Person { get; set; }
}

映射文件如下图:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Person, Employee.DAL" table="`Person`"  >
    <id name="PersonID" column="`PersonId`" type="int">
      <generator class="native" />
    </id>
    <property type="string" name="Forename" column="`Forename`" />
    <property type="string" name="Surname" column="`Surname`" />
    <property type="int" name="Age" column="`Age`" />
    <one-to-one name="Employee" class="Employee" cascade="all" />
  </class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Employee, Employee.DAL" table="`Employee`" >
    <id name="PersonID" column="`PersonId`">
      <generator class="foreign">
        <param name="property" >Person</param>
      </generator>
    </id>
    <property type="string" length="30" name="PayRollNo" column="`PayRollNo`" />
    <property type="int" name="Holidays" column="`Holidays`" />
    <one-to-one name="Person"  class="Person" constrained="true" />
  </class>
</hibernate-mapping>

【问题讨论】:

    标签: nhibernate nhibernate-mapping one-to-one


    【解决方案1】:

    将 session.Save() 方法放入事务中。或者调用 save 方法后使用 session.Flush() 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-29
      • 1970-01-01
      相关资源
      最近更新 更多