Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a Projection in NHibernate?

Tags:

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

like image 742
stiank81 Avatar asked Jan 20 '11 12:01

stiank81


People also ask

How do I write a query in NHibernate?

Simple LINQ Queries are treated as NHibernate queries, but you have to connect them to repositories. Connect entities with Repository and IRepository to reduce its complexities. And it will be more structured. Let me know if I have taken you to right track or not.

Is NHibernate an ORM?

NHibernate is a popular, fast growing ORM with a helpful community of seasoned developers. Used in thousands of commercial and open source projects.

Why is NHibernate used?

NHibernate is an ORM (Object Relational Mapper). Its purpose is to map objects in your OO application to tables in a database for persistence. Why would you need it? Because it can save you from writing a lot of tedious ADO.NET code.

What is QueryOver?

QueryOver is a strongly-typed version of Criteria, and is more NHibernate specific. Pretty much anything you can do in ICriteria can be done with QueryOver.


2 Answers

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

like image 175
Andrey Avatar answered Sep 21 '22 21:09

Andrey


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.

like image 34
Antoine Aubry Avatar answered Sep 22 '22 21:09

Antoine Aubry