RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Hibernate Criteria API: Multi-Criteria Search Made Easy : Page 4

The Hibernate Criteria API, a powerful and flexible alternative to traditional HQL queries, makes writing all those tricky multi-criteria search functions—as well as dynamic queries in general—much easier.


QBE: Query by Example

Another interesting approach is QBE. Basically, you instantiate one of your business objects with the corresponding values coming from the search screen. Hibernate then builds the corresponding query using the non-null field values:

    Accommodation accommodationEx = new Accommodation();
	 // and so on

    Example example 
                = Example.create(accommodationEx)

    return getSession()

You can also fine-tune the way the QBE query is built in many ways, for example:

  • Queries may be case sensitive or insensitive (ignoreCase()).
  • Zero-valued fields may be ignored (excludeZeroes()).
  • Certain properties may be ignored (excludeProperty()).
  • And so on...

The interesting thing here is that, if you have a lot of text fields in your multi-criteria search screen, you can use QBE to initialize the text values, and then use Expression-based criterion objects to add further constraints. In fact, the Example object is just another Criterion. So you can mix and match QBE and Criteria-based querying as you need.

Powerful, Elegant and Definitely Worth a Try

The Hibernate criteria API is a powerful and elegant library, which is well adapted for implementing multi-criteria search functionalities where queries must be built on the fly. Using it in appropriate circumstances will result in cleaner, clearer, more reliable, and more maintainable code. Try it out!

John Ferguson Smart is project director at Aacom, a French IT firm specializing in J2EE solutions. He holds a PhD in computer science from the University of Aix-Marseille, France. His specialties are J2EE architecture and development and IT project management.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date