单个SQL连接进行QueryOver多对多 [英] QueryOver Many to Many with a single SQL join

查看:112
本文介绍了单个SQL连接进行QueryOver多对多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个实体,多对多链接. (产品和用户) 我想按用户限制产品:

I've got 2 entities, linked many-to-many. (Product & User) I want to restrict Products by Users:

User userAlias = null;
query.JoinAlias(product => product.Users, () => userAlias)
  .Where(() => userAlias.Id == currentUser.Id);

它是生成的SQL代码:

It's generated SQL code:

SELECT this_.Id as y0_
FROM   [Product] this_
       inner join UserToProduct users5_
         on this_.Id = users5_.Product_id
       inner join [User] useralias3_
         on users5_.User_id = useralias3_.Id
....

在位置"中,我仅使用user_id,不需要第二次加入.

In "Where" i use only user_id and i don't need second join.

如何通过单个SQL联接编写查询(通过QueryOver)?

How I can write the query(by QueryOver) with a single SQL join ?

推荐答案

这可能有帮助吗?我有与UsersRoles类似的设置

This may help? I have a similar setup with UsersRoles

Role roleAlias = null;
var roles = _session.QueryOver<UsersRole>().JoinAlias(x => x.Role, () => roleAlias, JoinType.LeftOuterJoin).Where(
            x => x.User.UserId == userId).List();

产生以下内容:

SELECT this_.UsersRolesId         as UsersRol1_32_1_,
       this_.UserId               as UserId32_1_,
       this_.RoleId               as RoleId32_1_,
       rolealias1_.RoleId         as RoleId27_0_,
       rolealias1_.RoleName       as RoleName27_0_,
       rolealias1_.Active         as Active27_0_,
       rolealias1_.DateCreated    as DateCrea4_27_0_,
       rolealias1_.LastUpdated    as LastUpda5_27_0_,
       rolealias1_.RoleSettingsId as RoleSett6_27_0_
FROM   UsersRoles this_
       left outer join Roles rolealias1_
         on this_.RoleId = rolealias1_.RoleId
WHERE  this_.UserId = 'a7eec4eb-21cc-4185-8847-a035010e779f' /* @p0 */

这篇关于单个SQL连接进行QueryOver多对多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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