由于该HQL语句需要查询一个String类型的prefix,所以该HQL语句查询后返回的类型为List<String>,如果要查询两个及以上字段时,则返回类型为List<Object[]>,然后遍历List集合,把相应的字段强转为对应的类型即可。
public List<String> findByHQL(String provinceName,String regionName,String yys) throws BaseException {
try {
StringBuffer hql = new StringBuffer();
hql.append("select ps.prefix from PhoneSegment ps where ps.province.name=" + "'"+provinceName+"'");
if(StringUtils.isNotBlank(regionName)){
hql.append(" and ps.region.name = "+ "'"+regionName+"'");
}
if(StringUtils.isNotBlank(yys)){
String[] strs = yys.split(",");
String phoneTypes = "";
for (int i = 0; i < strs.length; i++) {
phoneTypes += Enum.valueOf(PhoneType.class,strs[i]).ordinal()+",";
}
phoneTypes = phoneTypes.substring(0, phoneTypes.length()-1);
hql.append(" and phoneType in("+ phoneTypes +")");
}
return baseDao.find(hql.toString());
} catch (Exception e) {
log.error(e.getMessage(),e);
throw new BaseException(e);
}
}