3
Analyze the following situation: will come in request the information of the country that can be {0, 1, 2}
. The replacement method will look like this:
Nomenclature of the country parameter:
- 0: Bring all countries;
- 1: National only (Brazil)
2: Only international
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 will have to do this for about 10 fields. I would have a more viable way to do this validation and avoid so many if
?
The other fields/parameters just need to perform the exite validation to add more conditions in the AND (String hql += "and ....")
.
There’s a data field I’ll need to perform BETWEEN
.
What are these other 10 camps? Would they be from countries as well?
– Dherik
@Dherik, no. They would be normal fields, with the exception of
between
on the registration date. They may come null or with the information onString
.– DNick