繁体   English   中英

NHibernate JoinQueryOver和延迟加载

[英]NHibernate JoinQueryOver and Lazy Loading

我有一个查询

var query = _session.QueryOver<TEntity>().JoinQueryOver<PropertyMultTable>(p => p.Properties).Where(propertyPredicate).List();

该查询生成下一个SQL

 select this_.BaseEntity_id as BaseId0_1_ ,
        this_1_.Label as Label0_1_ ,
        this_1_.Description as Descript3_0_1_ ,
        this_1_.CreatedDate as CreatedD4_0_1_ ,
        this_.Width as Width2_1_ ,
        this_.Height as Height2_1_ ,
        this_.Duration as Duration2_1_ ,
        propertymu1_.id as id4_0_ ,
        propertymu1_.Name as Name4_0_ ,
        propertymu1_1_.DateTimeValue as DateTime2_5_0_ ,
        propertymu1_2_.IntegerValue as IntegerV2_6_0_ ,
        propertymu1_3_.DecimalValue as DecimalV2_7_0_ ,
        propertymu1_4_.StringValue as StringVa2_8_0_
 from   [Video] this_
        inner join [BaseEntity] this_1_ on this_.BaseEntity_id = this_1_.BaseId
        inner join [PropertyMultTable] propertymu1_ on this_.BaseEntity_id = propertymu1_.BaseEntity_id
        left outer join DateTimeValues propertymu1_1_ on propertymu1_.id = propertymu1_1_.PropertyMultTable_id
        left outer join IntegerValues propertymu1_2_ on propertymu1_.id = propertymu1_2_.PropertyMultTable_id
        left outer join DecimalValues propertymu1_3_ on propertymu1_.id = propertymu1_3_.PropertyMultTable_id
        left outer join StringValues propertymu1_4_ on propertymu1_.id = propertymu1_4_.PropertyMultTable_id
 where  ( propertymu1_2_.IntegerValue >= 459144
          and propertymu1_2_.IntegerValue <= 691982
        )

但是我只想获取实体,而没有属性。 因此,我需要这样的SQL:

 select this_.BaseEntity_id as BaseId0_1_ ,
        this_1_.Label as Label0_1_ ,
        this_1_.Description as Descript3_0_1_ ,
        this_1_.CreatedDate as CreatedD4_0_1_ ,
        this_.Width as Width2_1_ ,
        this_.Height as Height2_1_ ,
        this_.Duration as Duration2_1_
 from   [Video] this_
        inner join [BaseEntity] this_1_ on this_.BaseEntity_id = this_1_.BaseId
        inner join [PropertyMultTable] propertymu1_ on this_.BaseEntity_id = propertymu1_.BaseEntity_id
        left outer join DateTimeValues propertymu1_1_ on propertymu1_.id = propertymu1_1_.PropertyMultTable_id
        left outer join IntegerValues propertymu1_2_ on propertymu1_.id = propertymu1_2_.PropertyMultTable_id
        left outer join DecimalValues propertymu1_3_ on propertymu1_.id = propertymu1_3_.PropertyMultTable_id
        left outer join StringValues propertymu1_4_ on propertymu1_.id = propertymu1_4_.PropertyMultTable_id
 where  ( propertymu1_2_.IntegerValue >= 459144
          and propertymu1_2_.IntegerValue <= 691982
        )

或者甚至更好,例如:

    select distinct this_.BaseEntity_id as BaseId0_1_ ,
        this_1_.Label as Label0_1_ ,
        this_1_.Description as Descript3_0_1_ ,
        this_1_.CreatedDate as CreatedD4_0_1_ ,
        this_.Width as Width2_1_ ,
        this_.Height as Height2_1_ ,
        this_.Duration as Duration2_1_
 from   [Video] this_
        inner join [BaseEntity] this_1_ on this_.BaseEntity_id = this_1_.BaseId
        inner join [PropertyMultTable] propertymu1_ on this_.BaseEntity_id = propertymu1_.BaseEntity_id
        left outer join IntegerValues propertymu1_2_ on propertymu1_.id = propertymu1_2_.PropertyMultTable_id
 where  ( propertymu1_2_.IntegerValue >= 459144
          and propertymu1_2_.IntegerValue <= 691982
        )

我可以使用Fluent NHibernate吗? 感谢您的答复。

也许使用Future()

var query =
  _session.QueryOver<TEntity>()
    .JoinQueryOver<PropertyMultTable>(p => p.Properties)
    .Future()
    .Where(propertyPredicate).List();

这是使用HQL的方法。

var hqlQuery=string.Format( "select v from Video as v inner join v.BaseEntity 
as be inner join v.PropertyMultTable as pmt left outer join pmt.IntegerValues 
as iv where be.BaseEntity_id=v.BaseId and be.BaseEntity_id=pmt.BaseEntity_id and 
pmt.Id=iv.PropertyMultTable_id and iv.IntegerValue >={0} and iv.IntegerValue
<={1}", 459144,691982);

请注意,当完成内部连接时,Im假定属性名称与上面查询中提到的名称相同。 它们应该与您的财产中提到的完全一样,否则您将获得休眠异常。

如果不起作用,则发布整个类图。

您可以按以下方式运行此查询:

session.CreateQuery(hqlquery).List<Video>();

暂无
暂无

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

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