views:

35

answers:

1

Imagine a simple web page allowing you to search Bookings like this:

picture of web page

  • If the Booking Number field is not populated I want to return all rows.
  • If the Booking Number field is populated I only want to return that row.

I have been using a pattern in Hibernate (with an underlying MySQL database) to accomplish this that goes something like:

AND (:bookingNum IS NULL OR (:bookingNum IS NOT NULL AND :bookingNum = booking.bookingNumber))

I've recently been seeing some weird performance issues and that got me wondering if there is a better pattern to use here. I'm sure non-Hibernate queries must have similar issues.

What do others do in this siutation?

Thanks,

D.

+1  A: 

Use a Hibernate criteria query, adding the predicate only if bookingNum isn't null.

tpdi
I'm using HQL not Criteria. I can do similar using StringBuilder and IF statements. But I was hoping for something more elegant. Too hopeful?
Damo