Mohammed R. El-Khoudary
Mohammed R. El-Khoudary

Reputation: 1203

Hibernate "Like" expressions on two sides Criteria

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!

Upvotes: 0

Views: 1121

Answers (2)

AppHouze
AppHouze

Reputation: 206

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?

Upvotes: 1

hmrojas.p
hmrojas.p

Reputation: 582

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.

Upvotes: 0

Related Questions