问题描述
我最近偶然发现了一个问题,我必须使用类似谓词构建JPA标准,该谓词接受自定义类型,该类型是。我正在处理的自定义类型只是 String
包含一些额外的信息。
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.
我现在必须为JPA做同样的事情,但Hibernates实现,这是调用时创建:
I have to do the same now for JPA, but the Hibernates implementation of Predicate
, the LikePredicate
which is created when calling:
似乎相当复杂。
我需要的是:
有没有人做过这样的事情还能提供一些提示吗?
Has any one done such a thing yet and can offer me some hints?
谢谢!
推荐答案
使用帮助器方法解决了该问题,其中 CustomString
是自定义 UserType
:
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());
}
这篇关于像谓词一样自定义来处理自定义用户定义的类型(Hibernate UserType)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!