親子関係の一部である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>
これは問題ないようですが、リストにいくつかのSalesVoucherItemを含む新しいSalesVoucherクラスをデータベースに挿入するテストを実行すると、SQLエラーがスローされます:「itemposition列にNULLを挿入できません。列はnullを受け入れません」
リスト内のインデックスは必須データであり、データベースでそのように宣言する必要があるため、NHはその位置にNULLを挿入しようとしているようですが、これは私には意味がありません。何か案は?助けてくれてありがとう!ご挨拶。