How to use LIKE in sleep mode for an integer data type?

I need to do Restrictions.like("sequenceNo", "%" + Integer.valueOf(sequenceNo.trim()) + "%") .

The sequenceNo field is an integer, but the value of the sequenceNo parameter is a string. My problem is that I get java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer exception java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer . For some reason, I really need to make my parameter a string data type. When I tried this in SQL to LIKE an integer, it works.

Please, help. Thanks.

+6
source share
5 answers

You cannot add restrictions to the Criteria property for this purpose, since during the selection, the specified property value must be executed in accordance with the type of the field specified in the Entity class.

However, the solution will use the SQLRestriction criteria by casting. I tested and it works.

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

To get a list, you would like to do below

 List ls = yourDetachedCriteriaObj.getExecutableCriteria(yourSession).list(); // iterate over list or do whatever. 
+6
source

I am ashamed, but I made the following workaround with postgres

 crit.add(Restrictions.sqlRestriction(entry.getKey()+"::text like '%"+entry.getValue().replace("'", "''")+"%'")); 
+1
source

My postgres change

  cr34.add (Restrictions.sqlRestriction (cr34.getAlias ​​() + "_. field :: text like '%" + fieldVal + "%'"));
0
source

for integer search parameter

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

Try it...

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

Return error for inactive type:

Operator

does not exit: integer ~~ unknown TIP. The statement does not match the specified name and arguments. you may need to add castr of type castr.

so we can use this code:

 yourDetachedCriteriaObj.add(Restrictions.sqlRestriction(" sequenceNo ::text LIKE '%"+yourSequenceNumToSearch+"%' ")); 
0
source

All Articles