Analyze the following situation: request will provide country information that can be {0, 1, 2}
. The repository method will look like this:
Country Parameter Naming:
- 0: Bring all countries;
- 1: National only (Brazil)
-
2: International only
public List<View> getDto(Request request) { Session session = getSession(); session.beginTransaction(); String hql = "select v from view v where 1=1 "; if(0 != request.getCountry()) { String hql += "and country = :country"; } List<View> view = session.createQuery(hql, View.class).getResultList(); if(0 != request.getCountry()) { session.setParameter(":country", request.getCountry()); } return view; }
I'll have to do this for about 10 camps. Would you have a more feasible way to do this validation and avoid so many if
?
The other fields / parameters just need to perform exite validation to add more conditions in AND (String hql += "and ....")
.
There is also a date field that I need to perform a BETWEEN
.