SQL 'LIKE' operator in Hibernate Criteria API

后端 未结 5 1484
萌比男神i
萌比男神i 2020-12-16 14:55

I want to implement some universal filter with Hibernate Criteria. It should work like LIKE operator from SQL:



        
5条回答
  •  孤街浪徒
    2020-12-16 15:39

    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().

提交回复
热议问题