Java 在JPA CriteriaBuilder中使用正则表达式
我使用JPA CriteriaBuilder从MySQL数据库中选择类型为Java 在JPA CriteriaBuilder中使用正则表达式,java,mysql,regex,hibernate,jpa,Java,Mysql,Regex,Hibernate,Jpa,我使用JPA CriteriaBuilder从MySQL数据库中选择类型为MyEntity的实体,如下所示: String regExp = "(abc|def)" CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery query = cb.createQuery( MyEntity.class ); root = query.from( MyEntity.class ); predicates = new ArrayList&l
MyEntity
的实体,如下所示:
String regExp = "(abc|def)"
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery query = cb.createQuery( MyEntity.class );
root = query.from( MyEntity.class );
predicates = new ArrayList<Predicate>();
predicates.add( cb.like( root.<String>get( "name" ), regExp ) );
String regExp=“(abc | def)”
CriteriaBuilder cb=em.getCriteriaBuilder();
CriteriaQuery=cb.createQuery(MyEntity.class);
root=query.from(MyEntity.class);
谓词=新的ArrayList();
add(cb.like(root.get(“name”),regExp));
因此,查询结果应该包含name
值与给定regExp匹配的任何实体。但是结果列表总是空的。
将regExp更改为/(abc|def)/g
无效,添加通配符%
如何使模式匹配工作
或者:如何将本机MySQL REGEXP与CriteriaBuilder一起使用?JPA查询中的模式匹配仅限于
-任何字符。
-任何字符串%
REGEXP
在MySQL中有运算符语法(SELECT'a'REGEXP'a'
),因此不能与CriteriaBuilder.function()一起使用。恐怕最好是运行本机SQL查询
如果您使用的是Hibernate,那么您还有一个选择。您可以使用CriteriaBuilder.function()
运行,也许这段代码会有所帮助。我们必须在搜索中排除字符,我们使用Oracle。CriteriaBuilder(至少从2.1开始)将允许您调用函数
private static final Pattern UNDESIRABLES = Pattern.compile("[(){},.;!?<>%_-]");
private static final String UNDESIRABLE_REPLACEMENT = "";
...
我最近遇到了这个问题,并使用第一篇文章实现了hibernate mysql函数选项
为了帮助他人节省时间,我做了以下工作:
在hibernate中的自定义方言文件中设置函数:
public class MySQLDialect extends Dialect {
public MySQLDialect() {
super();
...
registerFunction("regexp", new SQLFunctionTemplate(StandardBasicTypes.INTEGER, "?1 REGEXP ?2"));
...
}
...
}
然后
在“标准生成器”部分中:
CriteriaBuilder builder = ...;
Pattern regexPattern = Pattern.compile("^[0-9]\\|[0-9]+");
Expression<String> patternExpression = builder.<String>literal(regexPattern.pattern());
Path<String> path = ... ;// regex comparison column
// regexp comes from the name of the regex function
// defined in the Mysql Dialect file above
Predicate theRegexPredicate = builder.equal(builder.function("regexp", Integer.class, path, patternExpression), 1);
CriteriaBuilder=。。。;
Pattern regexpatern=Pattern.compile(“^[0-9]\\\\\\\\\\[0-9]+”);
表达式patternExpression=builder.literal(regexperten.pattern());
路径路径=…;//正则表达式比较列
//regexp来自regex函数的名称
//在上面的Mysql方言文件中定义
谓词theRegexPredicate=builder.equal(builder.function(“regexp”,Integer.class,path,patternExpression),1);
然后在CriteriaBuilder查询中使用TheRegeExpredicate构造where子句。我尝试了您所说的,但它没有返回任何内容。你们能看一下票吗,也许我错过了什么?
public class MySQLDialect extends Dialect {
public MySQLDialect() {
super();
...
registerFunction("regexp", new SQLFunctionTemplate(StandardBasicTypes.INTEGER, "?1 REGEXP ?2"));
...
}
...
}
CriteriaBuilder builder = ...;
Pattern regexPattern = Pattern.compile("^[0-9]\\|[0-9]+");
Expression<String> patternExpression = builder.<String>literal(regexPattern.pattern());
Path<String> path = ... ;// regex comparison column
// regexp comes from the name of the regex function
// defined in the Mysql Dialect file above
Predicate theRegexPredicate = builder.equal(builder.function("regexp", Integer.class, path, patternExpression), 1);