I've found an elegant way to solve this problem. There is such sql opeartor as "coalese" (some explanations: http://www.1keydata.com/sql/sql-coalesce.html). It allows to pass any parameters to query and be sure that null
parameters will be ignored.
Example of my DAO interface (Spring data-jpa, MySQL):
@Query("select b from Book b where " +
"coalesce(b.name, '') like concat('%', :name, '%') " +
"and coalesce(b.author, '') like concat('%', :author, '%') " +
"and coalesce(b.category, '') like concat('%', :category, '%') " +
"and coalesce(b.publisher, '') like concat('%', :publisher, '%') " +
"and (b.available = :available1 or b.available = :available2)")
public Page<Book> findByParams(
@Param("name") String name
, @Param("author") String author
, @Param("category") String category
, @Param("publisher") String publisher
, @Param("available1") Boolean available1, @Param("available2") Boolean available2
, Pageable pageReq);
Hope this helps