2

我正在尝试使用 NHibernate 映射一个列表,该列表是父子关系的一部分。数据库结构为:

CREATE TABLE [dbo].[salesvouchers](
[ID] [int] IDENTITY(1,1) NOT NULL,
[id_vouchertype] [int] NOT NULL,
[id_salespoint] [int] NOT NULL) /*and a couple more fields*/
CREATE TABLE [dbo].[salesvouchersitems](
[ID] [int] IDENTITY(1,1) NOT NULL,
[id_salesvoucher] [int] NOT NULL,
[itemposition] [int] NOT NULL)

这两个表都通过具有外键的 id_salesvoucher 列相关联。问题是 itemposition 列是 SalesVoucherItem 列表的索引。将此映射文件用于 salesvoucher 类:

<class name="SalesVoucher" table="salesvouchers">
      <id name="Id" column="id">
         <generator class="native" />
      </id>
      <list name="Items" table="salesvouchersitems" lazy="true" cascade="all-delete-orphan">
         <key column="id_salesvoucher"/>
         <index column="itemposition"/>
         <one-to-many class="SalesVoucherItem"/>
      </list>
   <!--and more fields not relevant here-->
   </class>

这似乎很好,但是在进行测试以将新的 SalesVoucher 类插入到列表中的几个 SalesVoucherItem 的数据库中时,它会引发 SQL 错误:“无法将 NULL 插入 itemposition 列。列不接受空值”

似乎 NH 试图在该位置插入一个 NULL ,这对我来说没有意义,因为列表中的索引是强制性数据,应该在数据库中声明为这样。有任何想法吗?谢谢您的帮助!问候。

4

1 回答 1

1

您必须将 itemposition 列公开为属性并设置其值。NHibernate 无法为您做到这一点。

于 2012-06-28T09:48:08.670 回答