I'm trying to test my JPA native @Query with H2. My native query is as follows:
@Query(
value = "SELECT * FROM accounts " +
" WHERE 'account_name' LIKE LOWER(CONCAT('%', COALESCE(:searchTerm, ''), '%')) ",
countQuery = "SELECT count(*) FROM accounts " +
" WHERE 'account_name' LIKE LOWER(CONCAT('%', COALESCE(:searchTerm, ''), '%')) ",
nativeQuery = true
)
When writing a unit test, I'm getting the following H2 error:
Caused by: org.h2.jdbc.JdbcSQLException: Table "ACCOUNTS" not found; SQL statement: SELECT * FROM accounts WHERE 'aws_account_name' LIKE LOWER(CONCAT('%', COALESCE(?, ''), '%')) limit ? [42102-197]
I can fix the H2 error by changing my SQL syntax to put table name in double-quotes:
@Query(
value = "SELECT * FROM \"accounts\" " +
" WHERE 'account_name' LIKE LOWER(CONCAT('%', COALESCE(:searchTerm, ''), '%')) ",
countQuery = "SELECT count(*) FROM \"accounts\" " +
" WHERE 'account_name' LIKE LOWER(CONCAT('%', COALESCE(:searchTerm, ''), '%')) ",
nativeQuery = true
)
HOWEVER, then my MySQL (actual non-test environment) complains:
Caused by: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"accounts" WHERE 'account_name' LIKE LOWER(CONCAT('%', COALESCE('ab', ''), ' at line 1
How can I test this native query with MySQL and H2?
The reason I'm using native query instead of JPQL is because I need to search without case sensitivity and allow "contains" matching.