简体   繁体   English

NHibernate Query <>与QueryOver <>有什么区别?

[英]What is the difference between NHibernate Query<> vs QueryOver<>?

I just started with NHibernate (using SQLite) in my current project and I mostly used Query<> , because I was familiar writing db queries in Linq. 我刚开始使用NHibernate(使用SQLite)在我当前的项目中,我主要使用Query<> ,因为我很熟悉在Linq中编写数据库查询。

When I was confronted with some more complex queries, I did some research on QueryOver<> and figured that it should be favored over Query<> because "QueryOver syntax is NH specific" . 当我遇到一些更复杂的查询时,我对QueryOver<>进行了一些研究,并认为它应该优于Query<>因为“QueryOver语法是NH特定的” Also, there seems to be nothing that Query<> can do that QueryOver<> can't accomplish. 此外, Query<>似乎没有任何QueryOver<>无法完成的操作。

So I began replacing all usages of Query<> accordingly. 所以我开始相应地更换Query<>所有用法。 It wasn't long before I had the first "issue" where using Query<> seemed just more convenient. 不久之后,我遇到第一个“问题”,使用Query<>似乎更方便。 Example (select highest value from column CustomNumber in table BillingDataEntity ): 示例(从表BillingDataEntity CustomNumber列中选择最高值):

int result = Session.Query<BillingDataEntity>().Select(x => x.CustomNumber).OrderByDescending(a => a).FirstOrDefault();
int result = Session.QueryOver<BillingDataEntity>().Select(x => x.CustomNumber).OrderBy(a => a.CustomNumber).Desc.Take(1).SingleOrDefault<int>();

What I dislike is the need to explicitly cast the result to int and that the the Query<> version is just easier to read. 我不喜欢的是需要将结果显式地转换为int,并且Query <>版本更容易阅读。 Am i getting the query totally wrong, or in other words: Is there a better way to do it? 我的查询完全错了,换句话说:有更好的方法吗?

I took a look at the generated SQL output: 我看了一下生成的SQL输出:

NHibernate: select billingdat0_.CustomNumber as col_0_0_ from "BillingDataEntity" billingdat0_ order by billingdat0_.CustomNumber desc limit 1
NHibernate: SELECT this_.CustomNumber as y0_ FROM "BillingDataEntity" this_ ORDER BY this_.CustomNumber desc limit @p0;@p0 = 1 [Type: Int32 (0)]

What exactly am i looking at? 我究竟在看什么? Is this the "internal" (method dependent) query that NHibernate further translates into the actual database query? 这是NHibernate进一步转换为实际数据库查询的“内部”(依赖于方法)查询吗?

There are plenty of answers regarding QueryOver versus Query here on Stackoverflow but in a nutshell:- 在Stackoverflow上有很多关于QueryOver与Query的答案,但简而言之: -

QueryOver is a strongly-typed version of Criteria, and is more NHibernate specific. QueryOver是Criteria的强类型版本,更具体的NHibernate。 Pretty much anything you can do in ICriteria can be done with QueryOver. 您可以使用QueryOver完成ICriteria中的任何操作。 In the golden days of ICriteria NH2 you always had to cast, hence this is why now you need to cast at the end of the chain back to an int. 在ICriteria NH2的黄金时代,你总是需要施放,因此这就是为什么现在你需要在链的末端施放回到int。

LINQ (Query) is a standard query method that works on IQueryable that doesn't need explicit references to NHibernate and can be considered more ORM agnostic and therefore follows the linq standard. LINQ(Query)是一种标准查询方法,适用于IQueryable,不需要显式引用NHibernate,因此可以认为它更符合ORM,因此遵循linq标准。 As you rightly pointed out you do not need to cast to an int as you are selecting into the result the customNumber. 正如您正确指出的那样,当您选择customNumber时,不需要转换为int。

I would be very surprised for your simple example if the generated SQL was very different. 如果生成的SQL非常不同,我会对你的简单例子感到非常惊讶。

I was a big fan of QueryOver but as the Linq provider is getting more mature then 95% of my queries I use Query but for some Nhibernate specific stuff I resort back down to QueryOver . 我是QueryOver忠实粉丝,但随着Linq提供商变得越来越成熟,95%的查询我使用Query但是对于一些Nhibernate特定的东西我退回到QueryOver Either way I recommend using a profiling tool to see what you can live with. 无论哪种方式,我建议使用分析工具来查看您可以使用的内容。

Refs: Tradeoffs or versus and versus 参考: 权衡 对比

About your QueryOver version, I would have written : 关于你的QueryOver版本,我会写:

int result = Session.QueryOver<BillingDataEntity>()
               .Select(Projections.Max<BillingDataEntity>(x => x.CustomNumber))
               .SingleOrDefault<int>();

It seems quite readable, and the resulting SQL would be something like : 它似乎很可读,结果SQL将是这样的:

SELECT max(this_.CustomNumber) as y0_ FROM "BillingDataEntity" this_

Hope this will help 希望这会有所帮助

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

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