由于以下 Nhibernate 问题,我整天都在用头撞桌子。
每个银行账户都有一组(并且只有一组)与之关联的利率。银行账户表的主键BankAccountID也是外键,也是AccountRate表的主键。
public class BankAccount
{
public virtual int BankAccountId { get; set; }
public virtual string AccountName { get; set;}
public virtual AccountRate AccountRate {get;set;}
}
public class AccountRate
{
public virtual int BankAccountId { get; set; }
public virtual decimal Rate1 { get; set; }
public virtual decimal Rate2 { get; set; }
}
我有以下 BankAccount 的 HBM 映射:
<class name="BankAccount" table="BankAccount">
<id name ="BankAccountId" column="BankAccountId">
<generator class="foreign">
<param name="property">
AccountRate
</param>
</generator>
</id>
<property name ="AccountName" column="AccountName" />
<one-to-one name="AccountRate" class="AccountRate" constrained="true" cascade="save-update"/>
</class>
以及 AccountRate 的以下内容:
<class name="AccountRate" table="AccountRate">
<id name ="BankAccountId" column="BankAccountId">
<generator class="native" />
</id>
<property name ="Rate1" column="Rate1" />
<property name ="Rate2" column="Rate2" />
</class>
可以毫无问题地从数据库中读取现有的 BankAccount 对象。但是,当创建一个新的 BankAccount 时,插入语句失败:
Cannot insert the value NULL into column 'BankAccountId'
问题似乎是首先创建了子对象 AccountRate 。由于尚未从其 Parent 获得标识符,因此插入失败。
我认为我的说法是正确的,如果 BankAccount 上的 AccountRate 属性是一个集合,我可以使用以下内容吗?
Inverse=True
为了强制先插入父级。
谁能帮我这个?我真的不想使用集合,这些表之间只有单向的一对一关系。
谢谢
保罗