10

我必须做Restrictions.like("sequenceNo", "%" + Integer.valueOf(sequenceNo.trim()) + "%")

该字段sequenceNo是整数类型,但sequenceNo参数值是字符串。我的问题是我得到了一个例外java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer。由于某些原因,我真的必须将我的参数设置为字符串数据类型。当我在 SQL 中尝试使用 LIKE 整数时,它可以工作。

请帮忙。谢谢。

4

5 回答 5

9

You cannot add Criteria`s property restrictions for the purpose, as during fetching, property value specifiedwould be casted according to the 'field type' specified in Entity class.

However, a solution would be using SQLRestriction of criteria, to by pass casting. I have tested and this works.

yourDetachedCriteriaObj.add(Restrictions.sqlRestriction(" sequenceNo LIKE '%"+yourSequenceNumToSearch+"%' "));

To get list you would do like below

List ls = yourDetachedCriteriaObj.getExecutableCriteria(yourSession).list();
// iterate over list or do whatever.
于 2013-03-29T10:32:49.073 回答
2

我很惭愧,但我确实遵循了 postgres 的解决方法

crit.add(Restrictions.sqlRestriction(entry.getKey()+"::text like '%"+entry.getValue().replace("'", "''")+"%'"));
于 2014-02-28T14:03:00.440 回答
0
yourDetachedCriteriaObj.add(Restrictions.sqlRestriction(" sequenceNo LIKE '%"+yourSequenceNumToSearch+"%' "));

禁用类型转换的返回错误:

运算符不退出:整数~~未知提示:没有运算符与给定的名称和参数类型匹配。您可能需要添加更显式的类型转换。

所以,我们可以使用这段代码:

yourDetachedCriteriaObj.add(Restrictions.sqlRestriction(" sequenceNo ::text LIKE '%"+yourSequenceNumToSearch+"%' "));
于 2016-07-31T13:08:50.463 回答
0

对于整数搜索参数

条件 = session.createCriteria(demo.class).add(Restrictions.sqlRestriction("sequenceNo LIKE '%"+searchParameter+"%'"));

尝试这个...

于 2016-06-10T12:41:48.030 回答
0

我的 postgres 修改

cr34.add(Restrictions.sqlRestriction(cr34.getAlias()+"_.field::text like '%"+ fieldVal+"%'"));
于 2016-05-26T09:58:00.610 回答