NHibernate JoinQueryOver 和延迟加载

发布于 2024-10-09 10:47:22 字数 3522 浏览 1 评论 0原文

我有一个查询,

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 来做到这一点吗?感谢您的回复。

I have a query

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

this query generates the next 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
        )

but I want to get only the Entity, without the properties. So, I need SQL like this:

 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
        )

or, even much better, like this:

    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
        )

Can I do this with Fluent NHibernate? Thanks for the responses.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷荒 2024-10-16 10:47:22

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

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

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

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

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

here is how you can do it using 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);

note here when inner join is done Im assuming the property names are the same as mentioned in the query above. they should be the exact same thing as mentione din your property or you will get an nhibernate exception.

If it doesnt work post the entire class diagram.

you can run this query as follows:

session.CreateQuery(hqlquery).List<Video>();
浮云落日 2024-10-16 10:47:22

也许使用 Future()

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

Maybe using Future():

var query =
  _session.QueryOver<TEntity>()
    .JoinQueryOver<PropertyMultTable>(p => p.Properties)
    .Future()
    .Where(propertyPredicate).List();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文