有人可以更好地解释什么是“预测”吗?是在 nHibernate 中吗?
作为 nHibernate 及其实用程序库(Fluent nhibernate)的新用户,我正在努力学习足够多的知识,以应对一个好的数据库的危险。
我很难理解投影的概念。具体来说,它们到底是什么?
我已经对“什么是投影?”和“nHibernate 中的项目”进行了精确搜索,并且'nHibernate、投影、定义'等。我仍然很困惑。到目前为止最有帮助的帖子是这个其他StackOverflow问题和Colin Ramsay 的这篇博文。但我仍然很困惑。我对数据库的了解充其量还只是入门级。
我真的不明白投影是什么,为什么我想使用它们,它们正在完成什么,等等。我在博客文章中看到他正在使用它们来获取整数列表(我假设主键),以便他可以在不同的查询中使用它们,但这在其运行方式和原因方面有点模糊。
As a new user of nHibernate and its utility library, fluent nhibernate, I am trying to learn enough to be dangerous with a good database.
I am having an exceptionally great deal of difficulty understanding the concept of Projections. Specifically, What in the world are they?
I have literally done exact searches on 'What are projections?' and 'Projects in nHibernate' and 'nHibernate, Projections, Definition', etc. And I am still very confused. The most helpful posts so far are This other StackOverflow Question and This Blog Post by Colin Ramsay. But I am still vastly confused. My knowledge of databases is still entry-level at best.
I do not really understand what projections are, why I would want to use them, what they are accomplishing, etc. I see in the blog post that he is using them to get a list of integers (I presume Primary Keys) so that he can use them in a different query, but this is kind of nebulous in the way it is functioning and the why.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个实际的例子。
假设您有一家在线商店,并且您的域类之一是“Samsung”等
品牌
。这个类有大量与之相关的属性,可能是一个整数Identity
、一个Name
、一个自由文本Description
字段、一个对Vendor
对象,等等。现在,假设您想要显示一个菜单,其中包含在线商店中提供的所有品牌的列表。如果您只是执行
session.CreateCriteria().List()
,那么您确实会获得所有品牌。但是您还可以从数据库中获取所有长Description
字段和对Vendor
的引用,并且您不需要它们来显示菜单;您只需要名称
和身份
。从性能角度来看,从数据库中吸收所有这些额外数据会减慢速度,而且是不必要的。相反,您可以创建一个仅包含
Identity
和调用它的Name
的“投影”对象,例如NameIdentityPair
:您可以告诉NHibernate 通过告诉它将结果集转换到您的投影上来仅选择执行手头任务真正需要的数据:
现在您没有
Brand
列表,而是一个列表NameIdentityPair
s,并且 NHibernate 将只发出一条 SQL 语句,例如 SELECT b.Identity, b.Name from dbo.Brand b 来获取此投影,而不是大量 SQL 语句,获取水合Brand
对象所需的所有内容(例如,SELECT b.Identity、b.Name、b.Description from dbo.brand b left join dbo.vendor v .. ..
)。希望这有帮助。
Here's a practical example.
Let's say that you have an online store and one of your domain classes is a
Brand
like "Samsung". This class has a boatload of properties associated with it, perhaps an integerIdentity
, aName
, a free-textDescription
field, a reference to aVendor
object, and so on.Now let's say that you want to display a menu with a list of all the brands offered on your online store. If you just do
session.CreateCriteria<Brand>().List()
, then you are indeed going to get all of the brands. But you'll also have sucked all of the longDescription
fields and references toVendor
s from the database, and you don't need that to display a menu; you just need theName
and theIdentity
. Performance-wise, sucking all of this extra data down from the database slows things down and is unnecessary.Instead, you can create a "projection" object that contains just the
Identity
and theName
calling it, say,NameIdentityPair
:And you could tell NHibernate to only select the data that you really need to perform the task at hand by telling it to transform the result set onto your projection:
Now you don't have a list of
Brand
s but instead a list ofNameIdentityPair
s, and NHibernate will have only issued a SQL statement likeSELECT b.Identity, b.Name from dbo.Brand b
to obtain this projection, as opposed to a massive SQL statement that grabs everything necessary to hydrate aBrand
object (e.g.,SELECT b.Identity, b.Name, b.Description from dbo.brand b left join dbo.vendor v ....
).Hope this helps.
如果您熟悉 SQL,投影就是查询的 SELECT 子句,用于从可用结果中选择要返回的字段。
例如,假设您有一个
Person
,其中包含FirstName
、LastName
、Address
和Phone字段。如果您希望查询返回所有内容,则可以省略投影,这就像 SQL 中的 SELECT * FROM Person
一样。如果您只需要名字和姓氏,则可以使用FirstName
和LastName
创建一个投影 - 这将是SELECT FirstName, LastName FROM Person
用 SQL 术语来说。If you're familiar with SQL, a projection is the
SELECT
clause of a query, used to select which fields from the available results to return.For example, assume you have a
Person
withFirstName
,LastName
,Address
, andPhone
fields. If you want a query to return everything, you can leave off the projection, which is likeSELECT * FROM Person
in SQL. If you just want the first and last names, you would create a projection withFirstName
andLastName
-- which would beSELECT FirstName, LastName FROM Person
in SQL terms.您可以使用投影来调用 SUM、COUNT 等 SQL 函数,或选择单个字段而不返回实体。
“...仅检索一个或多个实体的属性,无需加载开销
实体本身处于交易范围内。这有时称为报告
询问;更正确的说法是投影。” [NHibernate 实践]
You can use projections to call sql functions like SUM, COUNT... or select single fields without return an entity.
"...Retrieving only properties of an entity or entities, without the overhead of loading
the entity itself in a transactional scope. This is sometimes called a report
query; it’s more correctly called projection." [NHibernate in Action]