NHibernate映射:简单加入外键

时间:2022-09-15 16:47:06

The Tables

Currencies
----------
CurrencyId INT IDENTITY PK
IsoCode4217 CHAR(3) -- "USD", "GBP", "EUR", etc

Users
----------
UserId INT IDENTITY PK
CurrencyId FK REFERENCES Currencies (CurrencyId)

The Mapping

The current application has a Currency object that needs the IsoCode4217 value. A Currency is not an entity in this application, it's its own weird hard-coded thing where I need to pass the IsoCode4217 value to a static function and get a Currency instance back. The database Currencies table is more of an enumeration table. So I figured I'd just implement an IUserType and off I'll go, but this does not work as I expect:

当前应用程序具有需要IsoCode4217值的Currency对象。货币不是这个应用程序中的实体,它是它自己奇怪的硬编码的东西,我需要将IsoCode4217值传递给静态函数并返回一个Currency实例。数据库Currencies表更多是枚举表。所以我想我只是实现一个IUserType然后我会去,但这不会像我期望的那样工作:

[hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"]
  [class
    name="BlahBlah.UserProfile, BlahBlah.Model"
    schema="Core"
    table="Users"
    lazy="false"]
    [id name="Identity" access="field.camelcase" column="UserId"]
      [generator class="native" /]
    [/id]
    [join
      schema="Core"
      table="Currencies" 
      fetch="join"]
      [key column="CurrencyId" property-ref="Currency" /]
      [property
        name="Currency"
        column="IsoCode4217" 
        type="BlahBlah.CurrencyUserType, BlahBlah.Model" /]
    [/join]
  [/class]
[/hibernate-mapping]

(I changed the angle brackets to regular brackets as I don't feel like thinking about what this little Markdown editor is going to do.)

(我将尖括号更改为常规括号,因为我不想考虑这个Markdown编辑器会做什么。)

The Ugly

The SQL that I would expect is

我期望的SQL是

SELECT
  u.UserId,
  c.IsoCode4217
FROM Users AS u
INNER JOIN Currencies AS c
   ON u.CurrencyId = c.CurrencyId

but instead NHibernate stubbornly gives me

但NHibernate固执地给了我

SELECT
  u.UserId,
  c.IsoCode4217
FROM Users AS u
INNER JOIN Currencies AS c
  ON u.UserId = c.CurrencyId -- DANG IT!

Is this really not supported, or am I overlooking something obvious? Is it possible to just "loop in" an extra column from another table for the purpose of a single entity's mapping?

这真的不受支持,还是我忽略了一些明显的东西?为了单个实体的映射,是否可以从另一个表中“循环”一个额外的列?

Thanks!

1 个解决方案

#1


Join mappings are a PITA! The problem here is that Hibernate expects a join mapping to map from a Parent table to a Child table, whereas you're joining from the child table to the parent table.

加入映射是PITA!这里的问题是Hibernate期望连接映射从Parent表映射到Child表,而您从子表连接到父表。

Essentially, join mappings were designed to make it easy to join on tables that have a one-to-one relationship and where the parent table is joining to a child table.

本质上,连接映射旨在使连接具有一对一关系的表以及父表连接到子表的表很容易。

I think in your situation you want to have a many-to-one mapping rather than a join

我认为在你的情况下你想要一个多对一的映射而不是连接

#1


Join mappings are a PITA! The problem here is that Hibernate expects a join mapping to map from a Parent table to a Child table, whereas you're joining from the child table to the parent table.

加入映射是PITA!这里的问题是Hibernate期望连接映射从Parent表映射到Child表,而您从子表连接到父表。

Essentially, join mappings were designed to make it easy to join on tables that have a one-to-one relationship and where the parent table is joining to a child table.

本质上,连接映射旨在使连接具有一对一关系的表以及父表连接到子表的表很容易。

I think in your situation you want to have a many-to-one mapping rather than a join

我认为在你的情况下你想要一个多对一的映射而不是连接