我的 Spring JPA 查询不适用于 H2
下面的这个查询不起作用,它会产生一个异常
@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",
当我使用 MySQL 数据库时,查询工作正常。但是现在我用的是H2数据库,突然不行了。为什么?我该如何解决这个问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用JPA会更好。
如果您仍然想使用nativeQuery,请像这样使用:
It would be better to use JPA.
If you still wanna use nativeQuery, use like this:
我在这里猜测,但参数语法“?1”可能对 H2 无效。
这里不需要使用本机查询,如果您使用 Jpa 语法,它应该可以工作。
另外,您根本不需要在此处指定查询 - Spring Jpa 应该为您生成查询。
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.