Java Reference
In-Depth Information
create a Projection for a property. When you execute this form of query, the list() method
returns a List of Object arrays. Each Object array contains the projected properties for that
row. The following example returns just the contents of the name and description columns
from the Product data. Remember, Hibernate is polymorphic, so this also returns the name
and description from the Software objects that inherit from Product .
Criteria crit = session.createCriteria(Product.class);
ProjectionList projList = Projections.projectionList();
projList.add(Projections.property("name"));
projList.add(Projections.property("description"));
crit.setProjection(projList);
List results = crit.list();
Use this query style when you want to cut down on network traffic between your appli-
cation servers and your database servers. For instance, if your table has a large number of
columns, this can slim down your results. In other cases, you may have a large set of joins
that would return a very wide result set, but you are only interested in a few columns. Lastly,
if your clients have limited memory, this can save you trouble with large datasets. But make
sure you don't have to retrieve additional columns for the entire result set later, or your opti-
mizations may actually decrease performance.
You can group your results (using SQL's GROUP BY clause) with the groupProperty projec-
tion. The following example groups the products by name and price:
Criteria crit = session.createCriteria(Product.class);
ProjectionList projList = Projections.projectionList();
projList.add(Projections.groupProperty("name"));
projList.add(Projections.groupProperty("price"));
crit.setProjection(projList);
List results = crit.list();
As you can see, projections open up aggregates to the Criteria API, which means that
developers do not have to drop into HQL for aggregates. Projections offer a way to work with
data that is closer to the JDBC result set style, which may be appropriate for some parts of
your application.
Query By Example (QBE)
In this section, because of the confusing terminology, we will refer to excerpts from our
demonstration code as “samples” rather than “examples,” reserving “example” for its peculiar
technical meaning in the context of QBE.
In QBE, instead of programmatically building a Criteria object with Criterion objects
and logical expressions, you can partially populate an instance of the object. You use this
instance as a template and have Hibernate build the criteria for you based upon its values.
This keeps your code clean and makes your project easier to test. The org.hibernate.
criterion.Example class contains the QBE functionality. Note that the Example class imple-
ments the Criterion interface, so you can use it like any other restriction on a criteria query.
For instance, if we have a user database, we can construct an instance of a user object,
set the property values for type and creation date, and then use the Criteria API to run a
Search WWH ::




Custom Search