I have been banging my head on the table all day with the next Nhibernate problem.
Each bank account has one (and only one) set of rates associated with it. The primary key of the bank account table, BankAccountID is also the foreign key and primary key in the AccountRate table.
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; }
}
I have the following HBM mappings for BankAccount:
<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>
and for AccountRate the following:
<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>
An existing BankAccount object can be read from the database without any problems. However, when a new BankAccount is created, the insert statement does not work with:
Cannot insert the value NULL into column 'BankAccountId'
The problem is that the child AccountRate is created first. Since he has not yet received the identifier from his parent, the insertion does not work.
, , , AccountRate BankAccount , ?
Inverse=True
.
- ? , "--".