Nhibernate 一对一映射问题与子对象插入错误

发布于 2024-09-01 01:40:37 字数 1736 浏览 9 评论 0原文

我整天都在用头撞桌子,因为下面的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若无相欠,怎会相见 2024-09-08 01:40:38

好的,我想我已经解决了这个问题。看来我的问题是与共享主键值的经典一对一关联。答案是通过良好的睡眠找到的,然后参考 'Nhibernate 的行动'

首先,有一些错误需要纠正。这需要修改类和 HBM 文件。

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

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 文件如下。

<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,并添加“一对一”标签来完成类之间的关系。

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

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

保罗

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.

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

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>  

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.

Paul

千鲤 2024-09-08 01:40:38

解决此问题的一种快速方法是使 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文