NHibernate - 改变子类型 [英] NHibernate - Changing sub-types

查看:17
本文介绍了NHibernate - 改变子类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何在 NHibernate 中改变行的子类型?例如,如果我有一个 Customer 实体和一个 TierOneCustomer 的子类,我需要将一个 Customer 更改为 TierOneCustomer,但 TierOneCustomer 应该与原始 Customer 实体具有相同的 Id (PK).

How do you go about changing the subtype of a row in NHibernate? For example if I have a Customer entity and a subclass of TierOneCustomer, I have a case where I need to change a Customer to a TierOneCustomer but the TierOneCustomer should have the same Id (PK) as the original Customer entity.

映射看起来像这样:

<class name="Customer" table="SiteCustomer" discriminator-value="C">
  <id name="Id" column="Id" type="Int64">
    <generator class="identity" />
  </id>
  <discriminator column="CustomerType" />
  ... properties snipped ...

  <subclass name="TierOneCustomer" discriminator-value="P">
    ... more properties ...
  </subclass>
</class>

我使用每个类层次模型一个表,因此使用普通 sql,这只是鉴别器 (CustomerType) 的 sql 更新并设置与类型相关的适当列的问题.我在 NHibernate 中找不到解决方案,所以希望得到任何指点.

I'm using the one-table per class hierarchy model, so using plain-sql, it'd be just a matter of a sql update of the discriminator (CustomerType) and set the appropriate columns relevant for the type. I can't find the solution in NHibernate, so would appreciate any pointers.

考虑到这个用例,我也在考虑模型是否正确,但在我走这条路之前,我想确保首先按照上述方式进行操作实际上是可能的.如果没有,我几乎肯定会考虑更改模型.

I'm also thinking whether the model is correct considering this use-case, but before I go down that route, I want to make sure doing as described above is actually possible in the first place. If not, I'll almost certainly think about changing the model.

推荐答案

简短的回答是肯定的,您可以使用 本机 SQL.

Short answer is yes, you can change the discriminator value for the particular row(s) using native SQL.

但是,我不认为 N​​Hibernate 打算以这种方式工作,因为鉴别器通常对 Java 层是不可见的",它的值应该根据持久化对象的类进行初始设置,而从不改变了.

However, I don't think NHibernate is intended to work this way, since the discriminator is generally "invisible" to the Java layer, where its value is supposed to be set initially according to the class of the persisted object and never changed.

我建议寻找一种更清洁的方法.从对象模型的角度来看,您正在尝试将超类对象转换为其子类类型之一,同时不更改其持久化实例的标识,这就是冲突所在(转换后的对象实际上不应该是一样的东西).两种替代方法是:

I recommend looking into a cleaner approach. From the standpoint of the object model, you're trying to convert a superclass object into one of its subclass types while not changing the identity of its persisted instance, and that's where the conflict is (the converted object isn't really supposed to be the same thing). Two alternative approaches are:

  • 根据原 Customer 对象中的信息新建一个 TierOneCustomer 实例,然后删除原对象.如果您依赖客户的主键进行检索,则需要注意新的 PK.

  • 改变你的方法,这样对象类型(鉴别器)就不需要改变了.您可以使用可以随时自由修改的属性,而不是依赖子类来区分 TierOneCustomer 和 Customer,即 Customer.Tier = 1.

以下是 Hibernate 论坛上的一些相关讨论,您可能感兴趣:

Here are some related discussions on the Hibernate Forums that may be of interest:

  1. 我们可以更新 Hibernate 中的鉴别器列吗
  2. 每类表问题:鉴别器和属性
  3. 将持久化实例转换为子类

这篇关于NHibernate - 改变子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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