我使用 Spring Data JPA 和 hibernate 作为持久性提供程序。PostgreSQL 9.1.5 是我的数据库。
询问:
@Query("select COUNT(u) from User u where u.enabled=true")
被自动翻译成
select count(user0_.username) as col_0_0_ from users user0_ where user0_.enabled=1
如您所见,“真”被“1”取代。Postgre 不接受此查询并抛出错误:
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [select count(user0_.username) as col_0_0_ from users user0_ where user0_.enabled=1]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query] with root cause
无需替换的查询工作正常。在 pgadmin 查询界面中,以下查询有效。
select count(user0_.username) as col_0_0_ from users user0_ where user0_.enabled=true
我该如何解决这个问题?