SQL 'LIKE' operator in Hibernate Criteria API
You can use DetachedCriteria also when the hibernate session is not present.
DetachedCriteria criteria = DetachedCriteria.forClass(Pojo.class);
criteria.add(Restrictions.like("column", value, MatchMode.ANYWHERE));
It will match the value anywhere in the column string. You can use different types of modes.
Reference page: https://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/criterion/MatchMode.html
DetachedCriteria:
- Detached criteria is very good alternate when the hibernate session is not present.
Using a DetachedCriteria is exactly the same as a Criteria except you can do the initial creation and setup of your query without having access to the session. When it comes time to run your query, you must convert it to an executable query with getExecutableCriteria(session)
.
This is useful if you are building complicated queries, possibly through a multi-step process, because you don't need access to the Session everywhere. You only need the Session at the final step when you run the query.
Under the hood, DetachedCriteria uses a CriteriaImpl which is the same class you get if you call session.createCriteria()
.
You can use like()
restriction criteria like this:
session = sessionFactory.openSession();
Criteria query = session.createCriteria(Pojo.class);
query.add(Restrictions.like("anycolumn", "anyvalue", MatchMode.START));
It will give you a list of strings that start with "anyvalue".