使用查询结束解决Nhibernate中的重复关联路径错误

使用查询结束解决Nhibernate中的重复关联路径错误,nhibernate,fluent-nhibernate,nhibernate-criteria,queryover,Nhibernate,Fluent Nhibernate,Nhibernate Criteria,Queryover,我有一些代码试图访问相同的关联路径两次,它们实际上是相同的别名,但因为我使用的是查询对象,所以它们位于两个不同的位置,我不确定如何获取别名 可能有些代码可以消除混淆: var privateBlogQuery = new BlogQuery() .ByUser(1) .RemoveHidden() .FetchUserDetails(); //<-------- In Blog Query object class: ------> /// Gets a

我有一些代码试图访问相同的关联路径两次,它们实际上是相同的别名,但因为我使用的是查询对象,所以它们位于两个不同的位置,我不确定如何获取别名

可能有些代码可以消除混淆:

var privateBlogQuery = new BlogQuery()
    .ByUser(1)
    .RemoveHidden()
    .FetchUserDetails();


//<-------- In Blog Query object class: ------>

/// Gets all the private blogs the user has permissions to view
public BlogQuery ByUser(int userId)
{
    var authorisedUsers = null;

    this.Left.JoinQueryOver(r => r.AuthorisedUsers, () => authorisedUsers)
        .Where(r => r.User.Id == userId);

    return this;
}

/// Removes all private blogs user has marked to be hidden
public BlogQuery RemoveHidden()
{
    var authorisedUsers = null;

    this.Left.JoinQueryOver(r => r.AuthorisedUsers, () => authorisedUsers)
        .Where(r => !r.IsHidden);

    return this;
}

/// Loads up details of all users who have permission 
/// to view the private blog
public BlogQuery FetchUserDetails()
{
    var users = null;
    var authorisedUsers = null;

    this.Left.JoinQueryOver(r => r.AuthorisedUsers, () => authorisedUsers)
        .Left.JoinQueryOver(r => r.User, () => users);

    return this;
}
var privateBlogQuery=new BlogQuery()
.ByUser(1)
.RemoveHidden()
.FetchUserDetails();
//
///获取用户有权查看的所有私有博客
按用户公开博客查询(int userId)
{
var-authorizedUsers=null;
this.Left.JoinQueryOver(r=>r.authorizedUsers,()=>authorizedUsers)
。其中(r=>r.User.Id==userId);
归还这个;
}
///删除用户标记为隐藏的所有私人博客
公共BlogQuery RemoveHidden()
{
var-authorizedUsers=null;
this.Left.JoinQueryOver(r=>r.authorizedUsers,()=>authorizedUsers)
.其中(r=>!r.IsHidden);
归还这个;
}
///加载具有权限的所有用户的详细信息
///查看私人博客
公共BlogQuery FetchUserDetails()
{
var用户=null;
var-authorizedUsers=null;
this.Left.JoinQueryOver(r=>r.authorizedUsers,()=>authorizedUsers)
.Left.JoinQueryOver(r=>r.User,()=>users);
归还这个;
}
有时我会单独使用所有3个条件,生成的sql正是我所需要的,只要它们单独使用,一切都很好

现在我需要将它们一起使用,同时nhibernate抛出一个异常
复制别名
,我在这三个函数上更改了别名,但随后出现了
复制关联路径
异常

通过谷歌搜索,我和我也发现了一些

问题是我正在使用查询对象,因此查询结束,我不确定如何在这里获取关联路径/别名

那么我该怎么做呢?

  • authorizedUsers
    设置为
    BlogQuery
    的成员变量,并使用标记/标志来了解
    ByUser
    RemoveHidden
    是否应该进行加入
  • 使用
    JoinAlias
范例

AuthorisedUser authorisedUser;
bool authorisedUsersJoined;

public BlogQuery RemoveHidden()
{
    if (!authorisedUsersJoined)
        this.Left.JoinAlias(r => r.AuthorisedUsers, () => authorisedUser);

    this.Where(() => !authorisedUser.IsHidden);

    return this;
}
FetchUserDetails与其他两个是互斥的,因为对关联进行过滤会阻止NH初始化关联。您需要使用过滤器进行子查询,然后查询结果ID并初始化

/// Loads up details of all users who have permission 
/// to view the private blog
public BlogQuery FetchUserDetails()
{

    this.Query = QueryOver.Of<Blog>()
        .WhereRestrictionOn(b => b.Id).IsIn(this.Query.Select(b => b.Id))
        .Fetch(r => r.AuthorisedUsers).Eager
            .ThenFetch(au => au.User).Eager;

    return this;
}
///加载具有权限的所有用户的详细信息
///查看私人博客
公共BlogQuery FetchUserDetails()
{
this.Query=QueryOver.Of()
.WhereRestrictionOn(b=>b.Id).IsIn(this.Query.Select(b=>b.Id))
.Fetch(r=>r.authorizedUsers)。急切
.ThenFetch(au=>au.User).Eager;
归还这个;
}
  • authorizedUsers
    设置为
    BlogQuery
    的成员变量,并使用标记/标志来了解
    ByUser
    RemoveHidden
    是否应该进行加入
  • 使用
    JoinAlias
范例

AuthorisedUser authorisedUser;
bool authorisedUsersJoined;

public BlogQuery RemoveHidden()
{
    if (!authorisedUsersJoined)
        this.Left.JoinAlias(r => r.AuthorisedUsers, () => authorisedUser);

    this.Where(() => !authorisedUser.IsHidden);

    return this;
}
FetchUserDetails与其他两个是互斥的,因为对关联进行过滤会阻止NH初始化关联。您需要使用过滤器进行子查询,然后查询结果ID并初始化

/// Loads up details of all users who have permission 
/// to view the private blog
public BlogQuery FetchUserDetails()
{

    this.Query = QueryOver.Of<Blog>()
        .WhereRestrictionOn(b => b.Id).IsIn(this.Query.Select(b => b.Id))
        .Fetch(r => r.AuthorisedUsers).Eager
            .ThenFetch(au => au.User).Eager;

    return this;
}
///加载具有权限的所有用户的详细信息
///查看私人博客
公共BlogQuery FetchUserDetails()
{
this.Query=QueryOver.Of()
.WhereRestrictionOn(b=>b.Id).IsIn(this.Query.Select(b=>b.Id))
.Fetch(r=>r.authorizedUsers)。急切
.ThenFetch(au=>au.User).Eager;
归还这个;
}

这是一个非常具体的解决办法,可能不适用于您的情况,但我在遇到它时,通过将表映射到一个架构绑定视图(该视图为meThis进行了连接)解决了这个问题,这是一个非常具体的解决办法,可能不适用于您的情况,但是,当我遇到它时,通过将我的表映射到为meHi执行连接的模式绑定视图,我能够绕过这个问题-我如何获取/设置
。Where(()=>!authorizedUser.ishiden)
授权加入时
为真?因为我需要设置那个条件,我需要知道是否已经执行了连接..我修复了代码。应该始终设置where条件,而不考虑条件hi-如何获取/设置
.where(()=>!authorizedUser.ishiden)
授权加入时
为真?因为我需要设置那个条件,我需要知道是否已经执行了连接..我修复了代码。无论条件如何,都应始终设置where条件