【问题标题】:nhibernate QueryOver select one table after joinnhibernate QueryOver在加入后选择一张表
【发布时间】:2018-04-04 19:04:15
【问题描述】:

所以我在选择父实体时遇到问题,因为这也会返回子实体。 目前我有以下实体:

public class Parent
{
    public virtual string KeyPart1 { get; set; }
    public virtual string KeyPart2 { get; set; }    
    public virtual string KeyPart3 { get; set; }
    public virtual string ParentProperty { get; set; }

    public virtual ISet<Child> Children { get; set; } = new HashSet<Child>();
}

public class Child
{
    public virtual string KeyPart1 { get; set; }
    public virtual string KeyPart2 { get; set; }    
    public virtual string KeyPart3 { get; set; }
    public virtual string KeyPart4 { get; set; }
    public virtual string ChildProperty { get; set; }
}

以及实际的查询:

Parent parentAlias = null;
Child childAlias = null;
var query = Session.QueryOver(() => parentAlias)
    .Left.JoinAlias(x => x.Children, () => childAlias)
    .Where(whereClause);

这会生成一条与此非常相似的 sql 语句:

SELECT
    this_.KeyPart1 as KeyPart11_5_4_,
    this_.KeyPart2 as KeyPart22_5_4_,
    this_.KeyPart3 as KeyPart33_5_4_,
    this_.ParentProperty as ParentProperty4_5_4_,
    childalias1_.KeyPart1 as KeyPart11_6_6_,
    childalias1_.KeyPart2 as KeyPart22_6_6_,
    childalias1_.KeyPart3 as KeyPart33_6_6_,
    childalias1_.KeyPart4 as KeyPart44_6_6_,
    childalias1_.ChildProperty as ChildProperty5_6_6_,
FROM
    Parent this_ 
left outer join
    Child childalias1_ 
        on this_.KeyPart1=childalias1_.KeyPart1 
        and this_.KeyPart2=childalias1_.KeyPart2 
        and this_.KeyPart3=childalias1_.KeyPart3 
WHERE
    (SomeWhereClause)

请注意,这将返回 Parent 和 Child 表。当

query.List()

运行时,它将检索所有的父母+每个父母的所有孩子,在最终结果列表中创建重复的父母条目。我只想检索所有的父母或生成类似于以下的 sql 语句:

SELECT
    this_.KeyPart1 as KeyPart11_5_4_,
    this_.KeyPart2 as KeyPart22_5_4_,
    this_.KeyPart3 as KeyPart33_5_4_,
    this_.ParentProperty as ParentProperty4_5_4_,
FROM
    Parent this_ 
left outer join
    Child childalias1_ 
        on this_.KeyPart1=childalias1_.KeyPart1 
        and this_.KeyPart2=childalias1_.KeyPart2 
        and this_.KeyPart3=childalias1_.KeyPart3 
WHERE
    (SomeWhereClause)

有没有人有任何关于使用 nhibernate 的 QueryOver API 做到这一点的提示?

【问题讨论】:

    标签: c# nhibernate orm queryover


    【解决方案1】:

    一种方法是,使用 Transformers.DistinctRootEntity 例如:

    var query = Session.QueryOver(() => parentAlias)
        .Left.JoinAlias(x => x.Children, () => childAlias)
        .Where(whereClause)
    
        // Here is the trick
        .TransformUsing(Transformers.DistinctRootEntity)
    

    在这篇 Andrew Whitaker 帖子中查看更多信息:

    QueryOver Series - Part 4: Transforming

    反之……

    好吧,我不会去的,其实我是去的,不要用这种方式。我真正证明的方法是:

    1. 从子级创建查询并加入父级(星型模式)
    2. 如果我们必须查询父级
      • 用于过滤子使用子选择
      • 为了批量加载孩子使用映射设置batch-size

    第一个很明显,而且因为我们查询星型模式,我们可以很方便地使用分页

    第二个需要一些调整来按孩子过滤父母:

    为了有效加载子集合,我们可以从批量大小映射中获益。检查这些:

    【讨论】:

    • 感谢工作,但它似乎创建了相同的查询,然后在检索数据后,nhibernate 过滤掉数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多