如何使用 Nhibernate QueryOver 连接两个非 relashinship 定义的列的两个表 [英] How to join Two tables of two non relashinship defined columns using Nhibernate QueryOver
问题描述
使用 NHibernate QueryOver
,我想使用在映射中未定义为关系的两列连接两个表.
Using NHibernate QueryOver
, I want to join two tables using two columns which are not defined in the mapping as a relationship.
例如这不是我的确切场景,但这可以解释
E.g. This is not my exact scenario but this can explain that
表格:
Employee(Id, Name, DepartmentId, SomeCode,Address)
Department (Id, Name, ,Code)
选择
SELECT *
FROM EMPLOYEE E
JOIN DEPARTMENT D
ON D.Code = E.SomeCode
有人可以告诉我如何使用 NHibernate QueryOver
执行此查询.请注意,Employee
中的SomeCode"和Department
中的Code"并未定义为关系.DepartmentId 是外键,我可以使用 JoinAlias
加入这些外键,但我希望以不同的方式使用它.
Can someone please tell me how to do this query using NHibernate QueryOver
. Note that "SomeCode" in Employee
and "Code" in Department
are not defined as a relationship. DepartmentId is the foreign key and I can join these using JoinAlias
but I want it in a somewhat different way.
推荐答案
有类似的问题,例如
答案是:
- 使用 HQL 和 CROSS JOIN(带 WHERE)
- 没有方法可以使用
QueryOver
/Criteria
)
- either use HQL and CROSS JOIN (with WHERE)
- There is NO way how to do that with
QueryOver
/Criteria
)
查看文档:
可能会出现多个类,导致笛卡尔积或交叉"连接.
Multiple classes may appear, resulting in a cartesian product or "cross" join.
from Formula, Parameter
from Formula as form, Parameter as param
因此在您的情况下使用 HQL,我们可以这样做:
So using HQL in your case, we can do it like this:
SELECT ...
FROM EMPLOYEE E, DEPARTMENT D
WHERE D.Code = E.SomeCode
...
但我建议:在代码中创建该映射.简单介绍多对一
关系.它将被延迟加载(仅在使用时)并且很好地满足我们的需求 - 在 QueryOver
中用作 Join
BUT I would suggest: create that mapping in the code. Simply introduce many-to-one
relation. It will be loaded lazily (only if used) and will nicely serve to our needs - be used in QueryOver
as a relation for Join
如果有这样的关系,如果这个存在于业务对象域中,我们不应该害怕使用它.我们可以通过安全等方式隐藏它...
If there is such relation, if this exist in the business object domain, we should not be scared to use it. We can hide it via security etc...
这篇关于如何使用 Nhibernate QueryOver 连接两个非 relashinship 定义的列的两个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!