Implement “Search results” using Hibernate

By wakao

Hibernate Criteria API: Multi-Criteria Search Made Easy

Powerful, Elegant and Definitely Worth a Try,

that’s what the article says & it’s mighty true. Have seen quiet some people constructing these kind of queries using StringBuffer & stuff, but this is waaaay more elegant :)

..
Criteria criteria
= session.createCriteria(Accommodation.class);
if (startDate != null) {
criteria.add(Expression.ge("availabilityDate",
startDate);
}
if (endDate != null) {
criteria.add(Expression.le("availabilityDate",
endDate);
}

This beats

if (startDate != null) {
queryBuf.append(firstClause ? " where ": " and ");
queryBuf.append("a.availabiliyDate >=:startDate");
parameters.put("startDate",startDate);
firstClause = false;
}

any time !!

One Response to “Implement “Search results” using Hibernate”

  1. celpjefscycle Says:

    Thanks for information.
    many interesting things
    Celpjefscylc

Leave a Reply