NHibernate JoinQueryOver和Lazy Loading

时间:2010-12-27 10:26:06

标签: c# nhibernate fluent-nhibernate

我有一个查询

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吗?感谢您的回复。

2 个答案:

答案 0 :(得分:0)

以下是使用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);

请注意,当内连接完成时我假设属性名称与上面的查询中提到的相同。它们应该与提及你的财产完全相同,否则你将得到一个nhibernate例外。

如果在整个班级图表后它不起作用。

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

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

答案 1 :(得分:0)

也许使用Future()

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