类似于QueryDSL的操作SimplePath

类似于QueryDSL的操作SimplePath

问题描述:

类似于此问题我想使用我自己的用户定义类型"AccountNumber"来执行SQL"like"操作

Similarly to this question I would like to perform an SQL "like" operation using my own user defined type called "AccountNumber".

定义列的字段的QueryDSL Entity类如下所示:

The QueryDSL Entity class the field which defines the column looks like this:

public final SimplePath<com.myorg.types.AccountNumber> accountNumber;

我尝试使用以下代码在SQL中实现赞"操作,但是在运行查询之前比较类型时会出错:

I have tried the following code to achieve a "like" operation in SQL but get an error when the types are compared before the query is run:

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组合实现这一目标?

Has anyone ever been able to achieve this using QueryDSL/JPA combination?

最后,我的同事给了我提示,请他们做以下事情:

In the end, I was given a tip by my colleague to do the following:

 if (pRegion != null) {
        expressionBuilder.and(Expressions.booleanTemplate("{0} like concat({1}, '%')", qBusinessEvent.accountNumber, pRegion));
 }

这似乎可以解决问题!