什么是NHibernate的一个投影?NHibernate

2023-09-04 01:01:01 作者:浊世浮生

我在那里我使用NHibernate来处理绑定到数据库中的一个项目。到目前为止,我主要使用的基本知识,当涉及到查询了。现在,我挣扎着一个更困难的查询,我发现NHibernate的新的部件。特别是我很好奇 SetProjection ,这似乎是在做查询时是非常重要的。

I have a project where I'm using NHibernate to handle bindings to the database. So far I have mostly been using the basics when it comes to queries. Now I'm struggling with a more difficult query, and I notice new parts of NHibernate. In particular I'm curious about SetProjection, which seems to be important when doing queries.

什么是投影,以及如何将我通常用它?我假设投影是一个通用术语,当涉及到数据库,所以欢迎你提供更多的一般性的回答太..

What is a Projection, and how will I typically use it? I am assuming that a projection is a general term when it comes to databases, so you are welcome to give more general answers too..

推荐答案

投影作为安托万说的是转型。在查询方面,它是:

Projection as Antoine said is transformation. In terms of query it is:

SELECT *PROJECTION* FROM Table

*投射* 是前pression进行数据转换。

*PROJECTION* is expression for data transformation.

例如:

SELECT * FROM ORDER

本标准等效是:

The Criteria equivalent would be:

List orders = session.createCriteria(Order.class).list();

没有投射在这里,我们把数据均无需转换。如果我们想要一个:

No projection here, we take data without transformation. If we want one:

SELECT NAME FROM PRODUCT

在这里,投影类进场。上面的查询可以改写成一个标准的查询为:

Here, the Projection class comes into play. The above query can be rewritten into a Criteria query as:

List products=session.createCriteria(Product.class)
     .setProjection(Projection.property(\"name\"))
     .list();

所以,我们项目中的所有行以单个项目:名称字段

有其他的预测: Projection.rowCount()例如(对于 COUNT(*)

There are other projections: Projection.rowCount() for example (for COUNT(*))