0

I'd like to use criteria and create something like this but with hibernate criterias:

WHERE concat(X, Y) like '%Some Value%'

I don't want to do it using query concatenation, but using Restrictions because I am making a dynamic library that gets the left and right side of the like operator? Am I clear? Please if not ask me to give more clarification.

Thank you!

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29

2 Answers2

1

Hope it may works.

Criteria crit = session.createCriteria(Table.class);
crit.add(Restrictions.ilike("param1 || param2", "%" + dniWithLetter + "%"));

If the above code not working, please go through the following link.

Can we concatenate two properties in Hibernate HQL query?

Community
  • 1
  • 1
AppHouze
  • 206
  • 3
  • 10
0

I think that the best solution for your problem is the following

...
Object[] params = {objX, objY, someValue};
Type[] typeOfParams = {Hibernate.STRING, Hibernate.STRING, Hibernate.STRING};
Criteria crit = session.createCriteria(Table.class);
crit.add(Restrictions.sqlRestriction("concat(?, ?) like '%?%'", params, typeOfParams));
...

When you use Restrictions.sqlRestriction, you can build a complex query with multiple parameters, you just have to set the parameters and type of each parameter, this is done with params array and typeOfparams array.

I hope this information helps you.

Good luck.

hmrojas.p
  • 562
  • 1
  • 5
  • 15