For these kinds of queries, I generally use a Criteria
query with a form object. I then check for null
in each passed form field, and if not null
, then add another Restriction
into the query, using that field. Using a Criteria
query keeps the Java code very clean and eliminates messy string concatenation. An example for your case:
// Form object
public class bookingSearchForm {
private String bookingId;
public getBookingId()...
public setBookingId()...
}
// Hibernate
Criteria criteria = getSession().createCriteria(Booking.class);
if(form.getBookingId() != null) {
criteria.add(Restrictions.eq("bookingId", form.getBookingId()));
}
criteria.list();