What is a Projection in NHibernate?

给你一囗甜甜゛ 提交于 2019-12-04 15:22:40

问题


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..


回答1:


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

SELECT *PROJECTION* FROM Table

*PROJECTION* is expression for data transformation.

Example:

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();

So we project all rows to single item: name field.

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




回答2:


I don't know about NHibernate, but in general, a projection is a transformation of a set into another set. In SQL, it is expressed as a SELECT.



来源:https://stackoverflow.com/questions/4746995/what-is-a-projection-in-nhibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!