O'Reilly logo

Harnessing Hibernate by James Elliott, Ryan Fowler, Timothy M. O'Brien

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The Property Factory

In the methods we’ve been looking at so far, you start with the criterion, projection, or order you’re interested in creating, and then supply the name of the property with which you want to work as a parameter. The Criteria API also lets you work in the opposite direction, starting with the property and then calling a method to build a criterion or projection based on it. The class org.hibernate.criterion.Property is a factory for creating the Property instances you use when you prefer this approach. Property defines a single static method, forName(), which you can invoke to create an instance representing a particular property. Once you’ve got that instance, you can call one of the following methods to create criteria, projections and orderings based on the property it represents. Here is a list of the more commonly useful methods; the ones we omit have to do with detached criteria and subqueries, which are beyond the scope of this book. When you want to learn about them, see the Advanced query options chapter of Java Persistence with Hibernate or Detached queries and subqueries in the online reference.

MethodParametersPurpose
asc NoneCreates an Order that will sort results in ascending order by the property.
avg NoneCreates a Projection that returns the average of the property’s values.
between Object min, Object maxCreates a Criterion that requires the property have a value between min and max.
count NoneCreates a Projection that returns the number of ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required