不能将MatchMode与double类型的变量一起使用

时间:2016-09-06 08:32:35

标签: spring hibernate datatables spring-4 hibernate-4.x

我正在对server-side实施dataTable过滤。在实现过滤的服务中,我想测试search条件是number;如果是,那么我实现基于类型double(工资)的bean属性的过滤,否则我根据String(用户名)类型的bean属性进行过滤:

@Override
@Transactional
public List<User> list(int start, int length, String search) {
    Criteria criteres = sessionFactory.getCurrentSession().createCriteria(User.class);
    if (!search.equals("")) {
        if (NumberUtils.isNumber(search))
            criteres.add(Restrictions.like("salary", Double.parseDouble(search)));
        else
            criteres.add(Restrictions.like("username", search, MatchMode.ANYWHERE));
    }
    criteres.setFirstResult(start);
    criteres.setMaxResults(length);
    criteres.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    @SuppressWarnings("unchecked")
    List<User> listUser = (List<User>) criteres.list();
    return listUser;
}

问题在于,如果我写:

if (!search.equals("")) {
    if (NumberUtils.isNumber(search))
        criteres.add(Restrictions.like("salary", String.valueOf(Double.parseDouble(search)), MatchMode.ANYWHERE));
    else
        criteres.add(Restrictions.like("username", search, MatchMode.ANYWHERE));
}

然后我收到警告错误:DataTables warning: table id=t_list - Ajax error. For more information about this error, please see http://datatables.net/tn/7

那么如何才能在double变量上创建“ like ”条件?

0 个答案:

没有答案