Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate replace string condition

I'm having some trouble to create a query where a necessary condition is to use a like into a field where I need to replace a special character. Something like this:

public List<MyClass> search(Myclass object){

Criteria cri = criteria(MyClass.class);
if(object.getName() != null){
    cri.add(Restrictions.sqlRestriction("replace("+cri.getAlias()+".name,"+object.getSpecialCharacter()+", '') like '"+object.getName()+"'"));  
}
if(dominio.getType()!= null){
    cri.add(Restrictions.eq("type", object.getType()));
}
if(dominio.getWorkspace() != null){
    cri.add(Restrictions.eq("workspace", object.getWorkspace()));
}
if(dominio.getStatus() != null){
cri.add(Restrictions.eq("status", object.getStatus()));
}

return cri.list();

}

With this code I've got the error below:

this_.ID_MYCLASS as ID_MYCLASS1_33_0_,
this_.NM_MYCLASS as NM_MYCLASS4_33_0_,
this_.ID_STATUS as ID_STATU5_33_0_,
this_.ID_TYPE as ID_TYPE_7_33_0_,
this_.ID_WORKSPACE as ID_WORKS8_33_0_ 
from KDTB_MYCLASS this_ 
where replace(this.name,'_', '') like 'COD' 
and this_.ID_WORKSPACE=?

ORA-00904: "THIS"."NAME": invalid identifier

What am I doing wrong?

Thanks in advance!

like image 924
tfm Avatar asked Feb 01 '26 17:02

tfm


1 Answers

Try this instead:

cri.add(Restrictions.sqlRestriction("replace({alias}.name,"+object.getSpecialCharacter()+", '') like '"+object.getName()+"'"));
like image 153
Vlad Mihalcea Avatar answered Feb 03 '26 06:02

Vlad Mihalcea