NHibernate一对多无法插入null键

时间:2013-11-20 08:05:01

标签: c# nhibernate

我正在尝试使用NHibernate。我有以下表格:

    CREATE TABLE person(person_id INTEGER IDENTITY(1,1) NOT NULL,  
    person_first_name VARCHAR(55) NOT NULL,  
    person_last_name VARCHAR(55) NULL,   
    person_contacted_number INTEGER NOT NULL, 
    person_date_last_contacted DATETIME NOT NULL,
    person_date_added DATETIME NOT NULL,  
    CONSTRAINT PK_person PRIMARY KEY (person_id));

    CREATE TABLE person_order(order_id INTEGER IDENTITY(1,1) NOT NULL,  
    order_person_id INT NOT NULL,  
    CONSTRAINT PK_person PRIMARY KEY (order_id)),
    CONSTRAINT FK_person_order_person FOREIGN KEY(order_person_id)
    REFERENCES person (person_id);

类:

    public class Person
    {
        public virtual int person_id { get; set; }
        public virtual String person_first_name { get; set; }
        public virtual String person_last_name { get; set; }
        public virtual int person_contacted_number { get; set; }
        public virtual DateTime person_date_last_contacted { get; set; }
        public virtual DateTime person_date_added { get; set; }
        public virtual ISet<PersonOrder> person_orders { get; set; }
    }
    public class PersonOrder
    {
        public virtual int order_id { get; set; }
        public virtual int order_person_id { get; set; }
        public virtual Person Person { get; set; }
    }

的hbm.xml:

  <class name="Person" table="`person`">
        <id name="person_id">
          <generator class="native"/>
        </id>
        <property name="person_first_name" />
        <property name="person_last_name" />
        <property name="person_contacted_number" />
        <property name="person_date_last_contacted" />
        <property name="person_date_added" type="LocalDateTime" />
        <set name="person_orders" table="`person_order`" cascade="all-delete-orphan" inverse="true">
          <key column="order_person_id"/>
          <one-to-many class="PersonOrder"/>
        </set>
  </class>

  <class name="PersonOrder" table="`person_order`">
     <id name="order_id">
      <generator class="native"/>
    </id>
    <many-to-one name="Person" column="order_person_id" cascade="save-update" />
  </class>

我想将Person保存到数据库(cascade):

var newPerson = CreatePerson(); // return new Person
newPerson.person_orders = new HashedSet<PersonOrder> {new PersonOrder()};
session.Save(newPerson);

并且无法插入错误:

[TestConsoleApplication.PersonOrder][SQL: INSERT INTO [person_order] (order_person_id) VALUES (?); select SCOPE_IDENTITY()]

我认为这对于班级的一对多映射并不正确。如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我想说,最可疑的问题可能是订单本身缺少父/人设置。

一旦我们不使用级联,NHibernate必须独立地知道所有设置。换句话说,这还不够*(简化示例,期望IList而不是person_orders的IEnumerable)*:

var newPerson = CreatePerson(); // return new Person
newPerson.person_orders = new List<PersonOrder>();
// WRONG assignment
newPerson.person_orders.Add(new PersonOrder()); // first order
newPerson.person_orders.Add(new PersonOrder()); // second order

在这种情况下,我们的new PersonOrder不了解Person,即参考。所以我们必须确保它被分配两种方式(无论如何都是好的做法):

// improve it
var order1 = new PersonOrder
{
    Person = newPerson,
};
var order2 = new PersonOrder
{
    Person = newPerson,
};
newPerson.person_orders.Add(order1); // first order
newPerson.person_orders.Add(order2); // second order

确保这一点后,在session.Save(newPerson)和会话保存所有订单后......将发送正确的PersonId。

注意:尝试考虑使用级联,并使用<set>标记inverse="true"。老实说不记得没有使用过这种方法的场景

编辑:有点不同的映射 我建议改变映射类型。而不是iesi ISet (如果我们需要唯一性,这非常好)<set>,而不是标准IList<>和包映射。即:

Hbm是这样的:

<bag name="person_orders" table="`person_order`" 
     cascade="all-delete-orphan" inverse="true">
      <key column="order_person_id"/>
      <one-to-many class="PersonOrder"/>
</bag>

像这样的C#

public virtual IList person_orders {get;组; }

插入代码如下:

var newPerson = CreatePerson(); // return new Person
var order = new PersonOrder
{
    Person = newPerson,
};
newPerson.person_orders = new List<PersonOrder> {order};
session.Save(newPerson);

这将起作用(我几乎可以肯定,因为我只是尽可能地复制了)