【问题标题】:JPA replaces "true" by 1JPA 将“真”替换为 1
【发布时间】:2012-08-24 03:49:49
【问题描述】:

我使用 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

如您所见,“true”被“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

我该如何解决这个问题?

【问题讨论】:

  • 您使用哪个持久性提供程序(Hibernate?EclipseLink?OpenJPA?)以及User.enabled 的映射是什么样的?您的 ORM 配置中是否设置了正确的数据库方言?
  • 我使用 ProgressDialect 而不是 PostgreSQLDialect。感谢您的提示。

标签: spring-data-jpa


【解决方案1】:

确保您在 JPA 提供程序中使用正确的数据库方言。例如。在 Hibernate 中应该是:

org.hibernate.dialect.PostgreSQLDialect

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-14
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 2011-03-15
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    相关资源
    最近更新 更多