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

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

问题描述

我有一个现有的数据库,其中包含表 Transactions.我添加了一个名为 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 = ?'.但由于触发器,这失败了.如何配置我的映射,以便 NHibernate 在插入新的 TransactionSequence 表时不会尝试更新事务表?

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天全站免登陆