NHibernate一对一映射,其中第二个表数据可以为空 [英] NHibernate one-to-one mapping where second table data can be null

查看:109
本文介绍了NHibernate一对一映射,其中第二个表数据可以为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的数据库,其中有事务"表.我添加了一个名为TransactionSequence的新表,其中每个事务最终将只有一个记录.我们正在使用顺序表对给定帐户的交易进行计数.我已将此映射为一对一映射,其中TransactionSequence具有TransactionId的主键.

I have an existing database with the table Transactions in it. I have added a new table called TransactionSequence where each transaction will ultimately have only one record. We are using the sequence table to count transactions for a given account. I have mapped this as a one-to-one mapping where TransactionSequence has a primary key of TransactionId.

约束是事务表上有一个触发器而不是触发器,不允许更新已取消或已过帐的事务.

The constraint is that there is an instead of trigger on the transaction table does not allow updates of cancelled or posted transactions.

因此,当计算序列并保存事务时,NHibernate尝试发送有关事务的更新,例如'UPDATE Transaction SET TransactionId =? WHERE TransactionId =?'.但这由于触发而失败.我该如何配置映射,以便在插入新的TransactionSequence表时NHibernate不会尝试更新Transaction表?

So, when the sequence is calculated and the transaction is saved, NHibernate tries to send an update on the transaction like 'UPDATE Transaction SET TransactionId = ? WHERE TransactionId = ?'. But this fails because of the trigger. How can I configure my mapping so that NHibernate will not try to update the Transaction table when a new TransactionSequence table is inserted?

交易映射:

<class name="Transaction" table="Transaction" dynamic-update="true" select-before-update="true">
    <id name="Id" column="ID">
        <generator class="native" />
    </id>

    <property name="TransactionTypeId" access="field.camelcase-underscore" />
    <property name="TransactionStatusId" column="DebitDebitStatus" access="field.camelcase-underscore" />

    <one-to-one name="Sequence" class="TransactionSequence" fetch="join"
                 lazy="false" constrained="false">      
    </one-to-one>
</class>

和序列映射:

<class name="TransactionSequence" table="TransactionSequence" dynamic-update="true">
    <id name="TransactionId" column="TransactionID" type="Int32">
        <generator class="foreign">
            <param name="property">Transaction</param>
        </generator>
    </id>

    <version name="Version" column="Version" unsaved-value="-1" access="field.camelcase-underscore" />

    <property name="SequenceNumber" not-null="true" />

    <one-to-one name="Transaction" 
                class="Transaction" 
                constrained="true" 
                foreign-key="fk_Transaction_Sequence" />

</class>

任何帮助将不胜感激...

Any help would be greatly appreciated...

推荐答案

nhibernate中的一对一映射无法按您认为的方式工作.它的设计目的是使您拥有两个类,这些类在持久化到其对应的表时具有相同的主键.

One to one mapping in nhibernate doesn't work the way you think it does. It's designed so that you have two classes, which when persisted to their corresponding tables have the same primary keys.

但是您可以使它起作用,但是它并不漂亮.我将向您展示如何提供一些替代方案:

However you can make it work, but it's not pretty. I'll show you how then offer up some alternatives:

在您的交易hbml中:

In your Transaction hbml:

<one-to-one name="Sequence" class="TransactionSequence" property-ref="Transaction"/>

在您的序列html中:

In your Sequence html:

<many-to-one name="Transaction" class="Transaction" column="fk_Transaction_Sequence" />

应该做您想做的事情.注意属性引用.

This should do what you want it to do. Note the property-ref.

您要发布的下一个问题是询问您如何在一对一关联中延迟加载.答案是,您不能...您可以,但是它可能行不通.问题是您在序列表上有外键,这意味着nhibernate必须访问数据库以查看目标是否存在.然后,您可以尝试使用constrained ="true/false"来看看是否可以说服它延迟加载一对一关联.

The next question you're going to post on is going to ask how you get lazy loading on one-to-one associations. The answer is, you can't... well you can, but it probably won't work. The problem is that you have your foreign key on the sequence table, which means that nhibernate has to hit the database to see if the target exists. Then you can try playing around with constrained="true/false" to see if you can persuade it to lazily load the one-to-one association.

总而言之,这将完全浪费您的时间.

All in all, it's going to result in a total waste of your time.

我建议:

  1. 具有两个多对一关联.
  2. 与另一端的集合具有多对一关联.

从长远来看,这将为您节省很多头痛.

This will save you a lot of headaches in the long run.

这篇关于NHibernate一对一映射,其中第二个表数据可以为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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