0

我在 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>
4

1 回答 1

0

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

于 2011-01-24T13:22:33.347 回答