简体   繁体   English

NHibernate QueryOver,投影和别名

[英]NHibernate QueryOver, Projections and Aliases

I have an nhibernate issue where I am projecting the sql Coalesce function. 我在计划sql Coalesce函数时遇到一个nhibernate问题。

I am comparing two string properties having the same name, from two different entities. 我正在比较来自两个不同实体的两个具有相同名称的字符串属性。 In the resulting sql, the same property from only the first entity is being compared thus: 在生成的sql中,仅比较第一个实体的相同属性,因此:

var list = Projections.ProjectionList();
list.Add(
   Projections.SqlFunction("Coalesce",
    NHibernateUtil.String,
    Projections.Property<TranslatedText>(tt => tt.ItemText),
    Projections.Property<TextItem>(ti => ti.ItemText)));

var q = Session.QueryOver<TextItem>()
    .Left.JoinQueryOver(ti => ti.TranslatedItems);

Evaluating q results in this sql 在此sql中评估q结果

coalesce(this_.ItemText, this_.ItemText)

the this_ in the RHS needs to be an aliased table RHS中的this_必须是别名表

I can use Projections.Alias(Projections.Property<TranslatedText>(tt => tt.ItemText), "ttAlias") but am not sure how to map "ttAlias" in the JoinQueryOver . 我可以使用Projections.Alias(Projections.Property<TranslatedText>(tt => tt.ItemText), "ttAlias")但我不知道如何在“ttAlias”地图JoinQueryOver

I can create an alias there too, but can't see how to name it. 我也可以在那里创建一个别名,但是看不到如何命名。

TranslatedText ttAlias = null;
...
JoinQueryOver(ti => ti.TranslatedItems, () => ttAlias)

Aliases are variables in QueryOver, like you showed in the JoinQueryOver call. 别名是QueryOver中的变量,如JoinQueryOver调用中所示。 Alias names (strings) should not be needed in QueryOver, they are for Criteria queries. QueryOver中不需要别名(字符串),它们用于Criteria查询。

To the problem itself: I can't test it right now, but I think this should work: 对于问题本身:我现在无法测试,但是我认为这应该可行:

Projections.Property(() => ttAlias.ItemText)

I used this topic as a resource while writing a unit test. 在编写单元测试时,我将此主题用作资源。 This QueryOver works well and may help others with similar issues. 此QueryOver可以很好地工作,并且可以帮助其他遇到类似问题的人。 QueryOver still struggles with property mapping to transformers using expressions. QueryOver仍然难以使用表达式将属性映射到转换器。 It's technically possible to remove "Id" but IMHO it hinders clarity. 从技术上讲,可以删除“ Id”,但恕我直言,这会妨碍其清晰性。

The complete example is on GitHub 完整的示例在GitHub上

         String LocalizedName = "LocalizedName";
         //Build a set of columns with a coalese
         ProjectionList plColumns = Projections.ProjectionList();
         plColumns.Add(Projections.Property<Entity>(x => x.Id), "Id");
         plColumns.Add(Projections.SqlFunction("coalesce",
                                               NHibernateUtil.String,
                                               Projections.Property<Entity>(x => x.EnglishName),
                                               Projections.Property<Entity>(x => x.GermanName))
                                  .WithAlias(() => LocalizedName));

         ProjectionList plDistinct = Projections.ProjectionList();
         plDistinct.Add(Projections.Distinct(plColumns));


         //Make sure we parse and run without error
         Assert.DoesNotThrow(() => session.QueryOver<Entity>()
                                          .Select(plDistinct)
                                          .TransformUsing(Transformers.AliasToBean<LocalizedEntity>())
                                          .OrderByAlias(() => LocalizedName).Asc
                                          .Skip(10)
                                          .Take(20)
                                          .List<LocalizedEntity>());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM