我将 JPA2(EclipseLink 实现)与 Spring Data 一起使用,并使用规范模式来提供过滤器规范。
如何引入将映射到自定义 SQL 运算符的自定义表达式或谓词?我特别考虑使用 PostgreSQL FTS 扩展并添加如下条件:
SELECT ... WHERE ... AND column @@ 'ts query'
到我的规范实例。
最佳答案
有几种方法可以做到这一点:
For HQL in Hibernate
http://java-talks.blogspot.co.uk/2014/04/use-postgresql-full-text-search-with-hql.html
And for integrating Hibernate implementation with JPQL
http://metabroadcast.com/blog/hibernate-and-postgres-fts
Basically, what you need to do is
- Override your PostgreSQL Dialect
- Register functions with appropriate interpreters
- Call functions, when you need to use FTS
这是使用 EclipseLink https://wiki.eclipse.org/Introduction_to_EclipseLink_Expressions_%28ELUG%29 如何做到这一点的链接
基本上你需要做同样的员工,但 EclipseLink 风格
根据文档,它看起来像这样:
public class MyDatabasePlatform extends DatabasePlatform {
final private int FTS_ID = Interger.MAX_VALUE;
protected void initializePlatformOperators() {
super.initializePlatformOperators();
// Create user-defined function
ExpressionOperator fts = new ExpressionOperator();
fts.setSelector(FTS_ID);
List args = new ArrayList();
args.addElement(" @@ to_tsquery(");
args.addElement(")");
fts.printAs(args);
fts.bePrefix();
fts.setNodeClass(FunctionExpression.class);
// Make it available to this platform only
ExpressionOperator.registerOperator(FTS_ID, "fts");
addOperator(fts);
}
}
尽管我不确定该代码,但您需要对此进行测试。
关于spring - 如何使用 JPA QueryBuilder 提供自定义表达式或谓词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28672756/