问题
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!
回答1:
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());
}
来源:https://stackoverflow.com/questions/29145693/custom-like-predicate-to-handle-custom-user-defined-types-hibernate-usertype