带有外部表列的NHibernate过滤器,该表可能不包含在查询中

时间:2018-09-21 10:52:19

标签: c# nhibernate

考虑以下简化示例:

public class Entity
{
    public int Id { get; set; }

    public string Name { get; set; }
}

public class EntityPermission
{
    public int EntityId { get; set; }

    public int UserId { get; set; }
}

假设已使用hbm.xml文件映射了实体。

现在让我们说应用程序请求这样的实体列表:

Session.Query<Entity>().Where(x => Name == "whatever").ToList();

这将产生类似于以下内容的SQL语句:

SELECT 
    [ent].[Id],
    [ent].[Name]
FROM
    [dbo].[Entities] [ent]
WHERE
    [ent].[Name] = 'whatever'

我希望NHibernate为我生成以下SQL语句:

SELECT 
    [ent].[Id],
    [ent].[Name]
FROM
    [dbo].[Entities] [ent]
LEFT JOIN
    [dbo].[EntityPermissions] [perm]
ON
    [perm].[EntityId] = [ent].[Id]
WHERE
    [ent].[Name] = 'whatever'
AND
    [perm].[UserId] = :UserIdFilter.UserId

我知道我可以使用参数UserIdFilter来定义UserId,以使NHibernate自动将:UserIdFilter.UserId部分替换为设置的过滤器值(即,已设置当前用户或w / e),但是可以使NHibernate自动添加该LEFT JOIN来使过滤器有意义吗?

或者除了我在查询实体时必须手动添加的方法之外,还有别的优雅的替代方法吗?

0 个答案:

没有答案