【问题标题】:Why is a Hibernate JPQL Type Cast necessary for String为什么 String 需要 Hibernate JPQL Type Cast
【发布时间】:2012-01-16 13:21:47
【问题描述】:

我正在使用 Play!小型应用程序的框架。在模型中,我有以下查询:

public static ApplicationUser getByUserName(String userName) {
    return ApplicationUser.find("SELECT u FROM ApplicationUser u WHERE u.userName = ?", userName).first();
}

这与内存中的 DB H2 完美配合,但是当我使用 Postgres 时出现以下错误:

22:57:10,371 WARN  ~ SQL Error: 0, SQLState: 42883
22:57:10,371 ERROR ~ ERROR: operator does not exist: character varying = bytea
  Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
  Position: 167

当我像这样转换参数时:

ApplicationUser.find("SELECT u FROM ApplicationUser u WHERE u.userName = CAST(? AS string)", userName).first()

然后就可以了。但为什么这是必要的。这可能是 Hibernate 的错误吗?

更新: 我将播放版本从 1.2.4 降级到 1.2.3,现在它可以工作了。我认为问题可能出在随附的 postgres jdbc 驱动程序上。

更新二:问题仍未解决。我再次收到同样的查询错误:

ApplicationRole.find("byName", name).first();

错误:

JPAQueryException occured : Error while executing query SELECT u FROM ApplicationUser u WHERE u.userName = ?: ERROR: operator does not exist: character varying = bytea   Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.

application.conf 我有:

jpa.dialect=org.hibernate.dialect.PostgreSQLDialect

没有人遇到这个错误?

【问题讨论】:

    标签: hibernate playframework jpa-2.0 jpql


    【解决方案1】:

    你是否在 conf/application.conf 中启用了 postgresql 数据库方言?

    jpa.dialect=org.hibernate.dialect.PostgreSQLDialect

    【讨论】:

    • 查看我的更新。我已将游戏从 1.2.4 降级到 1.2.3,现在一切正常。
    【解决方案2】:

    尝试将 ? 替换为 (?1),如下所示:

    public static ApplicationUser getByUserName(String userName) {
        return ApplicationUser.find("SELECT u FROM ApplicationUser u WHERE u.userName = (?1)", userName).first();
    }
    

    我有一个类似的查询并且工作得很好。我假设 ApplicationUser 中的字段 userName 是字符串类型(以防万一!)

    此外,正如 Dominik 建议的那样,检查您的 jpa.dialect 是否设置为自动发现或 PosgreSQL。

    【讨论】:

    • 查看我的更新。我已将游戏从 1.2.4 降级到 1.2.3,现在一切正常。
    • 这对我有帮助。谢谢。抱歉回复晚了
    【解决方案3】:

    我遇到了完全相同的问题,1.2.4。

    User.find("byEmail", email).first()
    

    得到与您完全相同的转换错误,因此我不得不将其替换为(感谢 Pere)

    User.find("email = (?1)", email).first()
    

    有趣的是,

    User.find("byEmailAndPassword", email, password).first()
    

    仍然有效...

    【讨论】:

      猜你喜欢
      • 2020-08-27
      • 2015-07-18
      • 1970-01-01
      • 1970-01-01
      • 2010-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多