如何使用Nhibernate QueryOver联接两个非冲突定义列的两个表 [英] How to join Two tables of two non relashinship defined columns using Nhibernate QueryOver

查看:92
本文介绍了如何使用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.

推荐答案

还有类似的问题,例如

  • NHibernate (+ FluentNhibernate) : Join two detached tables
  • Hibernate Criteria Projection without mapped association of tables

答案是:

  1. 使用 HQL 和CROSS JOIN(与WHERE一起使用)
  2. > Criteria的方法
  1. either use HQL and CROSS JOIN (with WHERE)
  2. 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
...

但是我建议:在代码中创建该映射.简单介绍many-to-one关系.它会延迟加载(仅在使用时才加载),并且会很好地满足我们的需求-在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联接两个非冲突定义列的两个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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