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

半腔热情 提交于 2020-01-03 04:00:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!