jeudi 26 mars 2015

How to solve java.lang.StackOverflowError due to recursion while forming HQL query

I am getting StackOverflowError whenever iterations in loop goes higher. i am writing my logic like this:



public List<Vehicle> seacrhCar(Integer from, SearchDto searchDto,List<String> coveredZipcodes) {

String q = "from Vehicle where 1=1";
int i=1;

if(StringUtils.isNotBlank(searchDto.getFromYear()))
q+=" and year>='"+StringUtils.replace(searchDto.getFromYear().trim(), "'", "''")+"'";

if(StringUtils.isNotBlank(searchDto.getToYear()))
q+=" and year<='"+StringUtils.replace(searchDto.getToYear().trim(), "'", "''")+"'";

if(StringUtils.isNotBlank(searchDto.getManufacturer()))
q+=" and make='"+StringUtils.replace(searchDto.getManufacturer().trim(), "'", "''")+"'";

if(StringUtils.isNotBlank(searchDto.getCarModel()))
q+=" and model='"+StringUtils.replace(searchDto.getCarModel(), "'", "''")+"'";

if(StringUtils.isNotBlank(searchDto.getTrim()))
q+=" and trim='"+StringUtils.replace(searchDto.getTrim().trim(), "'", "''")+"'";

if(StringUtils.isNotBlank(searchDto.getMinPrice()))
q+=" and priceBaseMsrp>="+searchDto.getMinPrice().trim();

if(StringUtils.isNotBlank(searchDto.getMaxPrice()))
q+=" and priceBaseMsrp<="+searchDto.getMaxPrice().trim();

if(coveredZipcodes.size()>0) {
q+=" and (";
for(String zip : coveredZipcodes) {
q+="zipcode LIKE '%"+zip+"'";
q+=(i==coveredZipcodes.size())?")":" or ";
i++;
}
}
q+=" group by vehicle";


List<Vehicle> vehicles = sessionFactory.getCurrentSession().createQuery(q).setFirstResult(from).setMaxResults(10).setFlushMode(FlushMode.ALWAYS).list();
return vehicles;


This is the part in above code which is causing problem:



if(coveredZipcodes.size()>0) {
q+=" and (";
for(String zip : coveredZipcodes) {
q+="zipcode LIKE '%"+zip+"'";
q+=(i==coveredZipcodes.size())?")":" or ";
i++;
}
}


above logic is for searching vehicles from the Vehicle table based on multiple parameter, where one of the parameter is zipcodes. value of zipcodes are coming in a list which needs to be iterated and added to the query. size of zipcodeList (coveredZipcodes) depends on radius(distance) selected by user from his current zipcodes. eg, when a user selects 10 mile radius from his current zipcode, the no of zipcode will be, say 200, and as miles increases so does the no of zipcodes. up to 80-90 miles i am getting no error, but when i am selecting 100 miles or more, it throws StackOverflowError. i understood the problem after some research, but how do i solve this problem?


any help is appreciated.


Thanks!


Aucun commentaire:

Enregistrer un commentaire