Nhibernate带有子对象插入错误的一对一映射问题 [英] Nhibernate One-to-one mapping issue with child object insert error

查看:103
本文介绍了Nhibernate带有子对象插入错误的一对一映射问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于以下Nhibernate问题,我整天都在把头撞在桌子上.

i've being banging my head against the desk all day with the following Nhibernate problem.

每个银行帐户都有一套(只有一套)与之相关的利率.银行帐户表的主键BankAccountID也是外键,也是AccountRate表中的主键.

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; }
}

对于BankAccount,我具有以下HBM映射:

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>

以及AccountRate的以下内容:

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>

可以从数据库中毫无问题地读取现有的BankAccount对象.但是,当创建新的BankAccount时,insert语句将失败并显示;

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'

问题似乎是首先创建了子对象AccountRate.由于尚未从其父项获得标识符,因此插入失败.

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.

我认为我的说法是正确的,如果BankAccount上的AccountRate属性是一个集合,我可以使用以下代码吗?

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.

谢谢

保罗

推荐答案

好,我认为我已经解决了这个问题.看来我的问题是与共享主键值的经典一对一关联.晚上睡个好觉就找到了答案,然后参考行动中的休眠状态" .

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' .

首先,有许多错误需要更正.这需要同时修改类和HBM文件.

First there were a number of errors that needed correction. This required modification of both the classes and the HBM files.

首先,每个类都需要包含其他类类型的属性,因此我需要在AccountRate类中添加BankAccount属性.

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.

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; } 
    Public virtual BankAccount BankAccount {get;set;}
} 

我在BankAccount HBM文件中也犯了一个错误,我不应该将生成器类设置为外部.那应该在AccountRate类上.还需要从一对一链接中删除该约束.新的BankAccount HBM文件如下.

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.

<class name="BankAccount" table="BankAccount">  
<id name ="BankAccountId" column="BankAccountId">  
  <generator class="native">  
</id>  
<property name ="AccountName" column="AccountName" />  
<one-to-one name="AccountRate" class="AccountRate" cascade="all"/>  
</class>  

接下来,AccountRate HBM需要将生成器类设置为foreign,并添加一个一对一"标记以完成这些类之间的关系.

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.

<class name="AccountRate" table="AccountRate">          
<id name ="BankAccountId" column="BankAccountId">          
  <generator class="foreign">
        <param name="property">BankAccount</param>
  </generator>         
</id>          
<property name ="Rate1" column="Rate1" />          
<property name ="Rate2" column="Rate2" /> 
<one-to-one name="BankAccount" class="BankAccount" constrained="true" />       
</class>

感谢所有花时间研究此问题的人.我想这都是曲线的一部分.

Thanks to all those who took the time to look at this problem. I guess it's all part of the curve.

保罗

这篇关于Nhibernate带有子对象插入错误的一对一映射问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆