52

What is a Projection, in terms of database theory and NHibernate when using SetProjection()?

nvogel
  • 24,981
  • 1
  • 44
  • 82
DaveDev
  • 41,155
  • 72
  • 223
  • 385
  • possible duplicate of [What is a Projection in NHibernate?](http://stackoverflow.com/questions/4746995/what-is-a-projection-in-nhibernate) – philipxy Jul 15 '15 at 08:29

5 Answers5

51

Projection is one of the basic operations of Relational Algebra. It takes a relation and a (possibly empty) list of attributes of that relation as input. It outputs a relation containing only the specified list of attributes with duplicate tuples removed. In other words the output must also be a relation.

Example, if the relation R{A,B}, contains three tuples {1,10},{2,10},{3,20} then the projection of R over the attribute list {B} would contain 2 tuples: {10},{20}.

In short, projection is more or less equivalent to SELECT DISTINCT in SQL (excluding cases with nulls and duplicate columns).

nvogel
  • 24,981
  • 1
  • 44
  • 82
36

Very simply, it's a function which takes an input (e.g. a database row) and produces an output (e.g. one of the columns from the row, or perhaps some calculation based on multiple columns).

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • In Hibernate, where does the function that produces the output (e.g. the column) get executed? Does it happen on the SQL database server or does it happen on the application/hibernate server after the full row results are returned? Thanks. – KyleM Oct 30 '12 at 20:23
  • @KyleM: It's too long since I've used NHibernate to say for sure, but I'd expect it to be executed as part of the SQL. – Jon Skeet Oct 30 '12 at 22:11
  • Thanks.. that's what I think as well. I just remembered another way to check (hibernate query logging, which we usually don't use..), so I'll check and report back. – KyleM Oct 31 '12 at 00:20
  • @KyleM: The count projection definitely happens in the SQL, if you look at the query that is issued, it is select `count(columnName)` – mtyson Apr 28 '13 at 19:33
21

Projection means subset of columns in a query.

select x, y, z from YourTable 

x, y, z is the projection here.

Mohammed H
  • 6,880
  • 16
  • 81
  • 127
Bilal Anees
  • 211
  • 2
  • 3
2

If you are familiar with SQL or database tables: Projection refers to the number of fields/columns/attributes to return. Selection deals with number of rows/records to return. There are good video explanations here and here

Dave
  • 51
  • 3
  • While these links may answer the question, it is better to include the essential parts of the answer here and provide the links for reference. Link-only answers can become invalid if the linked page changes. – Cleb Jul 04 '15 at 20:02
2

In terms of hibernate, it's like specifying what columns to select. As opposed to letting the mappings determine what columns are fetched. This means you can specify sql functions, subqueries, a single column, or maybe all of the above via a ProjectionList. For example, if you wanted to count the rows in a table SetProjection(Projections.RowCount()).

dotjoe
  • 26,242
  • 5
  • 63
  • 77