QueryDSL 类操作 SimplePath
QueryDSL like operation SimplePath
与 this question 类似,我想使用我自己的名为 "AccountNumber".
的用户定义类型执行 SQL "like" 操作
QueryDSL 实体 class 定义列的字段如下所示:
public final SimplePath<com.myorg.types.AccountNumber> accountNumber;
我已尝试使用以下代码在 SQL 中实现 "like" 操作,但在查询 运行 之前比较类型时出现错误:
final Path path=QBusinessEvent.businessEvent.accountNumber;
final Expression<AccountNumber> constant = Expressions.constant(AccountNumber.valueOfWithWildcard(pRegion.toString()));
final BooleanExpression booleanOperation = Expressions.booleanOperation(Ops.STARTS_WITH, path, constant);
expressionBuilder.and(booleanOperation);
错误是:
org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [7!%%] did not match expected type [com.myorg.types.AccountNumber (n/a)]
有没有人能够使用 QueryDSL/JPA 组合实现这一目标?
您尝试过使用字符串常量吗?
Path<?> path = QBusinessEvent.businessEvent.accountNumber;
Expression<String> constant = Expressions.constant(pRegion.toString());
Predicate predicate = Expressions.predicate(Ops.STARTS_WITH, path, constant);
最后,我的同事给了我以下提示:
if (pRegion != null) {
expressionBuilder.and(Expressions.booleanTemplate("{0} like concat({1}, '%')", qBusinessEvent.accountNumber, pRegion));
}
这似乎可以解决问题![=11=]
好像有bug/ambiguity。就我而言,我需要按不同类型(String
、Number
)的几个字段进行搜索,例如SQL 看起来像:
SELECT * FROM table AS t WHERE t.name = "%some%" OR t.id = "%some%";
我的代码如下:
BooleanBuilder where = _getDefaultPredicateBuilder();
BooleanBuilder whereLike = new BooleanBuilder();
for(String likeField: _likeFields){
whereLike = whereLike.or(_pathBuilder.getString(likeField).contains(likeValue));
}
where.and(whereLike);
如果第一个 _likeFields
是 String
的类型 - 请求工作正常,否则抛出异常。
与 this question 类似,我想使用我自己的名为 "AccountNumber".
的用户定义类型执行 SQL "like" 操作QueryDSL 实体 class 定义列的字段如下所示:
public final SimplePath<com.myorg.types.AccountNumber> accountNumber;
我已尝试使用以下代码在 SQL 中实现 "like" 操作,但在查询 运行 之前比较类型时出现错误:
final Path path=QBusinessEvent.businessEvent.accountNumber;
final Expression<AccountNumber> constant = Expressions.constant(AccountNumber.valueOfWithWildcard(pRegion.toString()));
final BooleanExpression booleanOperation = Expressions.booleanOperation(Ops.STARTS_WITH, path, constant);
expressionBuilder.and(booleanOperation);
错误是:
org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [7!%%] did not match expected type [com.myorg.types.AccountNumber (n/a)]
有没有人能够使用 QueryDSL/JPA 组合实现这一目标?
您尝试过使用字符串常量吗?
Path<?> path = QBusinessEvent.businessEvent.accountNumber;
Expression<String> constant = Expressions.constant(pRegion.toString());
Predicate predicate = Expressions.predicate(Ops.STARTS_WITH, path, constant);
最后,我的同事给了我以下提示:
if (pRegion != null) {
expressionBuilder.and(Expressions.booleanTemplate("{0} like concat({1}, '%')", qBusinessEvent.accountNumber, pRegion));
}
这似乎可以解决问题![=11=]
好像有bug/ambiguity。就我而言,我需要按不同类型(String
、Number
)的几个字段进行搜索,例如SQL 看起来像:
SELECT * FROM table AS t WHERE t.name = "%some%" OR t.id = "%some%";
我的代码如下:
BooleanBuilder where = _getDefaultPredicateBuilder();
BooleanBuilder whereLike = new BooleanBuilder();
for(String likeField: _likeFields){
whereLike = whereLike.or(_pathBuilder.getString(likeField).contains(likeValue));
}
where.and(whereLike);
如果第一个 _likeFields
是 String
的类型 - 请求工作正常,否则抛出异常。