Hibernate also supports the ability to work with sql projections and sql aggregates. Instead of treating the results as an array of objects or a stream of objects you can return the result as a row with columns of data.
This is great for API driven applications as you DON"T have to retrieve the entire object graphs, you can decide which columns to bring back and return an array of structs with lightening speed by just using the asStruct()
modifier.
There are several projection types you can use which are great for doing counts, distinct counts, max values, sums, averages and much more.
The method in the criteria builder that will allow you to add projections is called withProjections()
. You will then use it's arguments to tell Hibernate what projection or aggregates to compile into the query. Here is the method signature:
Below are the available projections you can use from this method
The value of the arguments is one, a list or an array of property names to run the projection on, with the exception of id
and rowcount
which take a boolean
true.
Also, you can pass in a string separated with a : to denote an alias on the property when doing the SQL. The alias can then be used with any restriction the criteria builder can use.
If the :alias
is not used, then the alias becomes the property name.
Here is a detail overview of each projection type.
The name of the property to average or a list or array of property names to average on
The name of the property to count on or a list or array of property names to count on
Get a distinct count on a property or list of properties
Distinct is used to return only distinct (different) values from a property or list of properties
Which properties to group in the SQL statement.
Return an array of IDs of the current projected entity. Let's say we want all the user id's that have never logged in to the system. So we can use that to send them mails.
Return a max() sql projection on a a property or list of properties
Return a min() sql projection on a a property or list of properties
Return or a list of properties in the projection. Basically pick and choose in the select statement. The cool thing is that this can be a property or a full relationship, which can give you an array of properties and objects.
You can navigate relationships using the dot
notation.
Run a count( * )
on the projection alongside other projections.
Run a sql sum() on a one or many properties
Do a projection based on arbitrary SQL and SQL grouping strings. The value can be a single struct or an array of structs with the following pattern:
sql
- The raw sql to execute
alias
- The aliases to apply
property
- The property projected on
group
- What to group on if needed
This will produce the following SQL
Transform | Description | Example |
---|---|---|
avg
The name of the property to avg or a list or array of property names
withProjections(avg="salary")
count
The name of the property to count or a list or array of property names
withProjections(count="comments")
countDistinct
The name of the property to count distinct or a list or array of property names
withProjections(countDistinct="email")
distinct
The name of the property to do a distinct on, this can be a single property name a list or an array of property names
withProjections(distinct="email")
groupProperty
The name of the property to group by or a list or array of property names
withProjections(groupproperty="lastName")
id
Return the projected identifier value
withProjections(id=true)
max
The name of the property to max or a list or array of property names
withProjections(max="lastLogin")
min
The name of the property to min or a list or array of property names
withProjections(min="cid")
property
The name of the property to do a projected value on or a list or array of property names
withProjections(property="firstname")
rowCount
Do a row count on the criteria
withProjections(rowcount=true)
sum
The name of the property to sum or a list or array of property names
withProjections(sum="balance")
sqlProjection
Return projected value for sql fragment. Can accept a single config {sql,alias,property}, or an array of multiple configs.
withProjections(sqlProjection={sql="SELECT count(
) from blog where Year < 2006 and Author={alias}.Author",
alias="BlogPosts",
property="Author" })*
sqlGroupProjection
Return projected value for sql fragment with grouping. Can accept a single config( sql,alias,property,group}, or an array of multiple configs.
withProjections(sqlGroupProjection={sql="SELECT count(
) from blog where Year < 2006 and Author={alias}.Author",
alias="BlogPosts",
property="Author",
group="Author" })*
detachedSQLProjection
Creates a sqlProjection() based on Detached Criteria Builder