Search code examples
hibernatejakarta-eejpa-2.0hibernate-criteria

Custom like predicate to handle custom user defined types (Hibernate UserType)


I recently stumbled upon a problem where I have to build a JPA criteria with like predicate that accepts a custom type that is an implementation of UserType. The custom type I'm dealing with is nothing more than String wrapped with some additional info.

I've done it for Hibernate criteria with implementing the Criterion interface from Hibernate and it works fine.

I have to do the same now for JPA, but the Hibernates implementation of Predicate, the LikePredicate which is created when calling:

CriteriaBuilderImpl.like(Expression< String >, String)

seems quite complicated.

What I need is something like:

...like(Expression< MyUserType >, MyUserType)

Has any one done such a thing yet and can offer me some hints?

Thanks!


Solution

  • The problem was solved with a helper method where CustomString is the custom UserType:

    public static Predicate addLike(CriteriaBuilder cb, Path<CustomString> path, String val)
    {
        return cb.like(cb.lower(path.as(String.class)), val.toLowerCase());
    }