【问题标题】:NHibernate: getting object model with HQL using a join on tableNHibernate:使用表上的连接获取具有 HQL 的对象模型
【发布时间】:2011-04-04 11:29:18
【问题描述】:

我很难弄清楚如何使用表(非映射表)上的连接来获取映射对象。

表格:

DataForm: (Id, AliasName (FK), ...)
Customer: (Id, ...)
CustomerAliases: (PK AliasName, CustomerId (FK))

因此,每个客户的别名都是唯一的,并且客户可能有许多别名。

代码:

DataForm.Customer - this is what I need

那么如何在 getter 中编写 HQL? 由于 CustomerAliases 表只是一个别名列表,因此我没有将其映射到类,而是通过 Customer 访问。如何使用 AliasName 获取客户? 如果是正常的 SQL,我会这样做:

SELECT * From Customer c
INNER JOIN CustomerAliases ca ON ca.AliasName = 'AliasNameProvided'
WHERE ca.CustomerId = c.Id

【问题讨论】:

  • HSQL 是什么意思?为什么不映射别名?

标签: join nhibernate hql


【解决方案1】:

HQL 只能用于映射类。当您离开映射区域时,您需要使用与您问题中的 SQL 完全相同的 SQL。

我会映射它:

class Customer
{
  IList<string> AliasNames { get; private set; }
}

映射:

<class name="Customer">
  <!-- ..... -->
  <bag name="AliaseNames" table="CustomerAliases">
    <key column="CustomerId"/>
    <element column="AliasName"/>
  </bag>
</class>

查询:

from Customer c join c.AliasNames n
where n = :alias

【讨论】:

  • 所以我不需要使用带有映射的 CustomerAlias 类来工作吗?这正是我想要的。我会试试的。
  • 不,它只是一个字符串列表。不确定将别名作为主键是否那么容易。
  • 非常感谢它有效!我实际上已经映射了一个列表,但我不知道你可以加入 object.Something :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-28
  • 1970-01-01
相关资源
最近更新 更多