"org.jdbi.v3.core.statement.UnableToCreateStatementException: Undefined attribute for token '<endif>'

844 Views Asked by At

Method in Dao Interface

@RegisterRowMapper(MapMapper.class)
  @SqlQuery(
      "SELECT Table1.tenantId,Table1.sacTenantId, sacLogId,currentStep,status  from Table1 inner join Table2 on Table1.tenantId = Table2.tenantId where <if(tenantId)>Table1.tenantId = :tenantId and<endif> Table2.status = 'FAILED'")
  List<Map<String, Object>> getTenantFailedJobDetails(@Define("tenantId") @Bind("tenantId") String tenantId);

Error trace:

"level":"ERROR","categories":[],"msg":"Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.jdbi.v3.core.statement.UnableToCreateStatementException: Error rendering SQL template: 'SELECT Table1.tenantId,Table1.sacTenantId, sacLogId,currentStep,status from Table1 inner join Table2 on Table1.tenantId = Table2.tenantId where <if(tenantId)>Table1.tenantId = :tenantId and Table2.status = 'FAILED'' [statement:"null", arguments:{positional:{0:DUMMY-TENANT}, named:{tenantId:DUMMY-TENANT}, finder:[]}]] with root cause","stacktrace":["org.jdbi.v3.core.statement.UnableToCreateStatementException: Undefined attribute for token '' [statement:"null", arguments:{positional:{0:DUMMY-TENANT}, named:{tenantId:DUMMY-TENANT}, finder:[]}]"

What could be wrong with the if condition?

2

There are 2 best solutions below

0
On

The syntax you posted is stringtemplate4, so you need to use the stringtemplate 4 engine (which you select with the annotation that you posted). Otherwise you end up with the default engine which does support only very simply substitutions (and not st4 syntax).

1
On

To make if condition in jdbi query work I added annotation @UseStringTemplateEngine of package org.jdbi.v3.stringtemplate4 to the Dao method

If tenandId is not null then where clause will be

Table1.tenantId = :tenantId and Table2.status = 'FAILED' else where clause will be just

Table2.status = 'FAILED' One more information to add, for the else part annotation @AllowUnusedBindings package org.jdbi.v3.sqlobject.customizer is required