Using Spring EL in native Query

Viewed 30

Is it possible to use Spring Expression Language in a native query? I tried but faced exceptions. Also, I searched but couldn't find an obvious answer. For example:

@Query(value = "SELECT 1 FROM Table WHERE id=? and status= :#{T(com.test.constants.PrjConstants).TABLE_PENDING_STATUS_CODE}", nativeQuery = true)
public Integer isThereAnyPendingRecord(String id);
1 Answers

It is possible, but you cannot mix expressions. Also, if TABLE_PENDING_STATUS_CODE is a constant, you can just concatenate it, as that String must be final. Finally, why use SpEL here? Regular positional parameters would do the trick just fine.

@Query(value = "SELECT 1 FROM Table WHERE id=? and status= " + PrjConstants.TABLE_PENDING_STATUS_CODE, nativeQuery = true)
public Integer isThereAnyPendingRecord(String id);

Where TABLE_PENDING_STATUS_CODE must be final.

SpEL is not the tool for this IMO, but if you want to use it, that would be

@Query(value = "SELECT 1 FROM Table WHERE id=?#{[0]} and status= " + PrjConstants.TABLE_PENDING_STATUS_CODE, nativeQuery = true)
public Integer isThereAnyPendingRecord(String id);
Related