For this sort of dynamic queries using criteria is better.