2

多対多でリンクされた 2 つのエンティティがあります。(製品とユーザー) 製品をユーザー別に制限したい:

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 のみを使用し、2 番目の参加は必要ありません。

単一の SQL 結合で (QueryOver による) クエリを作成するにはどうすればよいですか?

4

1 に答える 1

1

これは役立つかもしれませんか?私は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 */
于 2012-04-19T18:41:02.937 に答える