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

标签 nhibernate one-to-one hbm

由于以下 Nhibernate 问题,我整天都在用头撞 table 。

每个银行账户都有一组(且只有一组)与之关联的利率。银行账户表的主键,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

为了强制先插入父对象。

谁能帮我解决这个问题?我真的不想用集合,这些表之间只有单向的一对一关系。

谢谢

保罗

最佳答案

好的,我想我已经解决了这个问题。看来我的问题是具有共享主键值的经典一对一关联。睡个好觉然后引用第 192-193 页 'Nhibernate in Action' 找到了答案。 .

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

首先,每个类都需要包含其他类类型的属性,因此我需要将 BankAccount 属性添加到 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; } 
    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>

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

保罗

关于Nhibernate 一对一映射问题与子对象插入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2799044/

相关文章:

hibernate - 如何使用 hibernate 将嵌套类映射到单个数据库表中?

c# - 将值为 System.Guid.Empty 的 NHibernate "translate"C# POCO Guid 属性保存为数据库 uniqueidentifier 字段的 NULL?

grails - Gorm一对一和一对多

java - 在 hibernate 中使用 onetoone 共享 pk

Hibernate 一对一实体关联与 3 个类之间的共享 PK

hibernate - 设置属性的 HQL 查询

hibernate - 生成的完整构造函数包含太多参数

c# - 是否有一种流畅的 NHibernate 方法可以索引所有外键而无需编码?

c# - List<int> 需要很长时间才能使用 Nhibernate Criteria 进行实例化

NHibernate QueryOver 和集合过滤