Reputation: 1091
I have to do Restrictions.like("sequenceNo", "%" + Integer.valueOf(sequenceNo.trim()) + "%")
.
The field sequenceNo
is integer type but the sequenceNo
param value is string. My problem is I get an exception java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
. For some reasons I really have to make my param a string data type. When I tried it in SQL to LIKE an integer it works.
Please help. Thanks.
Upvotes: 11
Views: 8584
Reputation: 1
for Integer search parameter
criteria = session.createCriteria(demo.class).add(Restrictions.sqlRestriction("sequenceNo LIKE '%"+searchParameter+"%' "));
Try this...
Upvotes: 0
Reputation: 970
yourDetachedCriteriaObj.add(Restrictions.sqlRestriction(" sequenceNo LIKE '%"+yourSequenceNumToSearch+"%' "));
return error for unabling type cast:
operator does not exit : integer ~~ unknown HINT: No operator matches the given name and argument tyeps(s). you might need to add explicitr type casts.
so, we can use this code:
yourDetachedCriteriaObj.add(Restrictions.sqlRestriction(" sequenceNo ::text LIKE '%"+yourSequenceNumToSearch+"%' "));
Upvotes: 0
Reputation: 2332
My postgres modification
cr34.add(Restrictions.sqlRestriction(cr34.getAlias()+"_.field::text like '%"+ fieldVal+"%'"));
Upvotes: 0
Reputation: 14865
i'm ashamed, but i did following workaround with postgres
crit.add(Restrictions.sqlRestriction(entry.getKey()+"::text like '%"+entry.getValue().replace("'", "''")+"%'"));
Upvotes: 2
Reputation: 627
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.
Upvotes: 9