Nhibernate 一对一映射问题与子对象插入错误
我整天都在用头撞桌子,因为下面的 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: 的 HBM 映射:
<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
为了强制首先插入父级。
谁能帮我解决这个问题吗?我真的不想使用集合,这些表之间只有单向的一对一关系。
谢谢
保罗
i've being banging my head against the desk all day with the following 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 a foreign key and the 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 the following for 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>
An existing BankAccount object can be read from the database with no problem. However, when a new BankAccount is created , the insert statement fails with;
Cannot insert the value NULL into column 'BankAccountId'
The issue appears to be that the child object , AccountRate is created first. Since it hasn't yet got an identifier from its Parent , the insert fails.
I think i'm correct in saying that if the AccountRate property on BankAccount was a collection i could use the following ?
Inverse=True
in order to force the parent to be inserted first.
Can anyone help me with this? i Really don't want to use a collection , there is only a unidirectional one-to-one relationship between these tables.
Thanks
Paul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,我想我已经解决了这个问题。看来我的问题是与共享主键值的经典一对一关联。答案是通过良好的睡眠找到的,然后参考 'Nhibernate 的行动' 。
首先,有一些错误需要纠正。这需要修改类和 HBM 文件。
首先,每个类都需要包含其他类类型的属性,因此我需要向 AccountRate 类添加 BankAccount 属性。
我还在 BankAccount HBM 文件中犯了一个错误,我不应该将生成器类设为外部类。那应该是在 AccountRate 类中。还需要从一对一链接中删除约束。新的 BankAccount HBM 文件如下。
接下来,AccountRate HBM 需要将生成器类设置为foreign,并添加“一对一”标签来完成类之间的关系。
感谢所有花时间研究这个问题的人。我想这都是曲线的一部分。
保罗
Ok , I think i've fixed the issue. It appears that my problem is a classic one-to-one association with shared primary-key values. The answer was found by a good nights sleep and then referring to page 192-193 of 'Nhibernate in Action' .
First there were a number of errors that needed correction. This required modification of both the classes and the HBM files.
Firstly each class needs to contain a property of the others class type, so i needed to add a BankAccount property to the AccountRate class.
I'd also made a error in the BankAccount HBM file , i shouldn't have made the generator class foreign. That should have been on the AccountRate class. The constraint also needed to be removed from the one-to-one linkage. The new BankAccount HBM file is as follows.
Next the AccountRate HBM needs the generator class set to foreign , and a 'one-to-one' tag added to complete the relationship between the classes.
Thanks to all those who took the time to look at this problem. I guess it's all part of the curve.
Paul
解决此问题的一种快速方法是使 BankAccountId 可为空。 NHibernate 应该在 BankAccount 表中插入一条 id 为空的记录,然后更新该 id。
在过去,我偶然发现了一种方法,可以让它使用正确的 id 而不是 null 进行初始插入。不幸的是,我一生都记不起我改变了什么。
One quick way around this would be to make the BankAccountId nullable. NHibernate should insert a record in the BankAccount table with a null id, then update the id.
In the past I've stumbled on a way to make it do the initial insert with the correct id instead of null. Unfortunately, I can't for the life of me remember what I changed.