Was given the below query to run against a DB2 database however it returns below error in my Spring Boot App.
I've researched that :COL_1_1001
should be some sort of named parameter or host variable(DB2) but have not been able to find a similar example of what I'm trying to do. I have only ever used ?1
to set a parameter using @Query
which to my understanding is for JPQL. Since I'm attempting a native SQL query here, not sure if this should work.
@Repository
public interface SomeRepository extends JpaRepository<Entity, EntityID> {
@Query(value="SELECT COL_1"
+ ",COL_2"
+ ",COL_3"
+ "FROM TABLE_A"
+ "WHERE COL_1 = :COL_1_1001", nativeQuery = true)
List<Entity> getEntityDetails();
}
[ERROR]~2019-09-19-07.26.30.347CDT~~~~~ o.a.c.c.C.[.[.[.[dispatcherServlet] Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: Named parameter not bound : COL; nested exception is org.hibernate.QueryException: Named parameter not bound : COL] with root cause org.hibernate.QueryException: Named parameter not bound : COL
You use variable COL_1_1001
in your query but do not provide it. You have to add an argument annotated with @Param
so the value can be injected into query.
Change:
@Query(value="SELECT COL_1"
+ ",COL_2"
+ ",COL_3"
+ "FROM TABLE_A"
+ "WHERE COL_1 = :COL_1_1001", nativeQuery = true)
List<Entity> getEntityDetails();
to:
@Query(value="SELECT COL_1"
+ ",COL_2"
+ ",COL_3"
+ "FROM TABLE_A"
+ "WHERE COL_1 = :col", nativeQuery = true)
List<Entity> getEntityDetails(@Param("col") String col); // it may not be a string - use the actual tyle of COL_1
For more info look into Spring Data documentation