Home > Mobile >  My Spring JPA queries do not work with H2
My Spring JPA queries do not work with H2

Time:02-25

This query below does not work and it generates me an exception

@Query(value = "SELECT * FROM account WHERE account_no = ?1",
nativeQuery = true)
Account findByAccountNo(String accountNo);
 "message": "could not prepare statement; SQL [SELECT * FROM account WHERE account_no = ?]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement",

When I used the MySQL database, the query worked fine. But now that I am using the H2 database, it suddenly does not work. Why? How do I fix this?

CodePudding user response:

It would be better to use JPA.

If you still wanna use nativeQuery, use like this:

@Query(value = "SELECT * FROM account WHERE account_no = :account_no",
nativeQuery = true)
Account findByAccountNo(@Param("account_no") String accountNo);

CodePudding user response:

I'm guessing here, but the parameter syntax "?1" probably is not valid for H2.

There's no need to use a native query here, and if you use Jpa syntax instead, it should work.

Also, you shouldn't need to specify a query here at all - Spring Jpa should generate the query for you.

  • Related