Nhibernate 使用单个SQL联接查询多对多
我有两个实体,多对多链接。(产品和用户) 我想按用户限制产品:Nhibernate 使用单个SQL联接查询多对多,nhibernate,queryover,Nhibernate,Queryover,我有两个实体,多对多链接。(产品和用户) 我想按用户限制产品: User userAlias = null; query.JoinAlias(product => product.Users, () => userAlias) .Where(() => userAlias.Id == currentUser.Id); 它生成了SQL代码: SELECT this_.Id as y0_ FROM [Product] this_ inner join Use
User userAlias = null;
query.JoinAlias(product => product.Users, () => userAlias)
.Where(() => userAlias.Id == currentUser.Id);
它生成了SQL代码:
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
....
在“Where”中,我只使用user_id,不需要第二次加入
如何使用单个SQL联接编写查询(通过QueryOver) 这会有帮助吗?我对UsersRoles有类似的设置
Role roleAlias = null;
var roles = _session.QueryOver<UsersRole>().JoinAlias(x => x.Role, () => roleAlias, JoinType.LeftOuterJoin).Where(
x => x.User.UserId == userId).List();
谢谢你帮我修好我的柱子。。Andrew:)为了澄清这一点,OP需要创建一个连接实体?谢谢你的回答,但是我没有关系“UserToProduct”的实体,我隐式地使用它,我不想创建它。我假设的products表中存储的用户ID也是这样吗?请下次发布您的个人信息,也许:)我不认为在QueryOver中没有不经过联接表就可以实现这一点。也许您是对的,我还没有找到一种方法。
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 */