Search code examples
sqljpasql-likequerydsl

QueryDSL like operation SimplePath


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

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

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

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);

The error is:

org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [7!%%] did not match expected type [com.myorg.types.AccountNumber (n/a)]

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


Solution

  • 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));
     }
    

    This seems to do the trick!