有人可以更好地解释什么是“预测”吗?是在 nHibernate 中吗?

发布于 2024-11-10 04:54:29 字数 651 浏览 5 评论 0原文

作为 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 技术交流群。

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

发布评论

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

评论(3

魂ガ小子 2024-11-17 04:54:29

这是一个实际的例子。

假设您有一家在线商店,并且您的域类之一是“Samsung”等品牌。这个类有大量与之相关的属性,可能是一个整数 Identity、一个 Name、一个自由文本 Description 字段、一个对Vendor 对象,等等。

现在,假设您想要显示一个菜单,其中包含在线商店中提供的所有品牌的列表。如果您只是执行 session.CreateCriteria().List(),那么您确实会获得所有品牌。但是您还可以从数据库中获取所有长Description字段和对Vendor的引用,并且您不需要它们来显示菜单;您只需要名称身份。从性能角度来看,从数​​据库中吸收所有这些额外数据会减慢速度,而且是不必要的。

相反,您可以创建一个仅包含 Identity 和调用它的 Name 的“投影”对象,例如 NameIdentityPair

public class NameIdentityPair
{
    public int Identity { get; set; }
    public string Name { get; set; }
}

您可以告诉NHibernate 通过告诉它将结果集转换到您的投影上来仅选择执行手头任务真正需要的数据:

var brandProjections = this.session.CreateCriteria<Brand>()
    .SetProjection(Projections.ProjectionList()
        .Add(Projections.Property("Name"), "Name")
        .Add(Projections.Property("Identity"), "Identity"))
    .SetResultTransformer(Transformers.AliasToBean<NameIdentityPair>())
    .List<NameIdentityPair>();

foreach (var brandProjection in brandProjections)
{
    Console.WriteLine(
        "Identity: {0}, Name: {1}", 
        brandProjection.Identity, 
        brandProjection.Name);
}

现在您没有 Brand 列表,而是一个列表NameIdentityPairs,并且 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 integer Identity, a Name, a free-text Description field, a reference to a Vendor 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 long Description fields and references to Vendors from the database, and you don't need that to display a menu; you just need the Name and the Identity. 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 the Name calling it, say, NameIdentityPair:

public class NameIdentityPair
{
    public int Identity { get; set; }
    public string Name { get; set; }
}

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:

var brandProjections = this.session.CreateCriteria<Brand>()
    .SetProjection(Projections.ProjectionList()
        .Add(Projections.Property("Name"), "Name")
        .Add(Projections.Property("Identity"), "Identity"))
    .SetResultTransformer(Transformers.AliasToBean<NameIdentityPair>())
    .List<NameIdentityPair>();

foreach (var brandProjection in brandProjections)
{
    Console.WriteLine(
        "Identity: {0}, Name: {1}", 
        brandProjection.Identity, 
        brandProjection.Name);
}

Now you don't have a list of Brands but instead a list of NameIdentityPairs, and NHibernate will have only issued a SQL statement like SELECT 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 a Brand object (e.g., SELECT b.Identity, b.Name, b.Description from dbo.brand b left join dbo.vendor v ....).

Hope this helps.

俯瞰星空 2024-11-17 04:54:29

如果您熟悉 SQL,投影就是查询的 SELECT 子句,用于从可用结果中选择要返回的字段。

例如,假设您有一个 Person,其中包含 FirstNameLastNameAddressPhone字段。如果您希望查询返回所有内容,则可以省略投影,这就像 SQL 中的 SELECT * FROM Person 一样。如果您只需要名字和姓氏,则可以使用 FirstNameLastName 创建一个投影 - 这将是 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 with FirstName, LastName, Address, and Phone fields. If you want a query to return everything, you can leave off the projection, which is like SELECT * FROM Person in SQL. If you just want the first and last names, you would create a projection with FirstName and LastName -- which would be SELECT FirstName, LastName FROM Person in SQL terms.

初心 2024-11-17 04:54:29

您可以使用投影来调用 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]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文