Good evening,
I’m sure I’m missing something about how to write queries in a Spring Boot application and would appreciate correction.
SQL query in h2-console:
SELECT customer_id FROM customers WHERE first_name = 'morton';
returns ‘1’
in Spring application:
@Query(nativeQuery = true, value = "SELECT 'customer_id' FROM customers WHERE 'first_name' = ?1") public Long findIdByName(String name);
returns null
Error text:
org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "SELECT 'customer_id' FROM CUSTOMERS WHERE 'first_name' = %[*]?"; expected "ALL, ANY, SOME"; SQL statement: SELECT 'customer_id' FROM customers WHERE 'first_name' = %? [42001-200]
What is wrong with the second query?
Let me know if you need the full stack trace.
Thanks
Advertisement
Answer
Remove single inverted commas from the query.
Change
SELECT 'customer_id' FROM customers WHERE 'first_name' = ?1
To
SELECT customer_id FROM customers WHERE first_name = ?1