How to search date field for a String using JPA Criteria API

前端 未结 3 959
孤街浪徒
孤街浪徒 2021-01-06 13:00

This is a question that spins off my other Question here . I thought it would be best put as a different question after someone(@Franck) pointed me to this link and this one

3条回答
  •  孤街浪徒
    2021-01-06 13:43

    This works in my case H2 (I use it for unit-tests), and I hope will work as well in Postgresql and Oracle, since TO_CHAR function seems to be cross-DB supported.

    Path path = ua.get(MyEntity_.timestamp);
    Expression dateStringExpr = cb.function("TO_CHAR", String.class, path, cb.literal("DD.MM.YYYY HH24:MI:SS"));
    predicates.add(cb.like(dateStringExpr, "%" + value + "%"));
    

    PS. MyEntity_ stands for metamodel generated for real MyEntity. You may read about Metamodels in Oracle docuemntation for Criteria API.

提交回复
热议问题