【问题标题】:ERROR: type "datetime" does not exist错误:类型“日期时间”不存在
【发布时间】:2016-12-14 02:04:43
【问题描述】:

我正在尝试将基于休眠的应用程序从 MySQL 迁移到 Postgres,但日期格式似乎存在问题

在我的实体中,我有

@Entity
@Table( name = "APP_USERS" )
public class User {

    @Id
    @GeneratedValue(generator="increment")
    @GenericGenerator(name="increment", strategy = "increment")
    private int id;

    private String username;
    private String email;
    private String password;
    private String firstname;
    private String lastname;
    private Timestamp joindate;
    ...

但我在创建表格时遇到错误

ERROR: type "datetime" does not exist

“showsql”中的sql是

Hibernate: create table APP_USERS (id integer not null, email varchar(255), firstname varchar(255), joindate datetime, lastname varchar(255), password varchar(255), username varchar(255), primary key (id))

我在本地安装了 postgresql-9.5.3-1,我使用的是 Hibernate 版本 5.0.10.Final

在 Google 上搜索该错误给我的结果很少。

【问题讨论】:

    标签: postgresql hibernate


    【解决方案1】:

    您的框架需要生成使用“时间戳”而不是“日期时间”的 DDL。

    当任何应用程序框架为 SQL DDL 生成错误的数据类型时,最可能的问题是它被配置为使用错误的数据库。在您的情况下,它可能仍配置为使用 MySQL。为 PostgreSQL 重新配置。

    【讨论】:

      【解决方案2】:

      PostgreSQL 没有日期时间数据类型。对于两者的组合,您需要“没有时区的时间戳”。

      https://www.postgresql.org/docs/9.5/static/datatype-datetime.html

      【讨论】:

      • 好的,你知道我应该在 Hibernate 中使用哪个类和/或注释来告诉它不要使用 datetime 而是使用 timestamp
      • 等等,现在我看到我有<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
      【解决方案3】:

      现在我明白了

      <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
      

      而不是

      <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
      

      【讨论】:

        猜你喜欢
        • 2022-01-20
        • 2018-03-20
        • 2019-03-30
        • 2023-03-09
        • 1970-01-01
        • 2020-10-07
        • 1970-01-01
        • 2021-03-28
        • 2010-10-05
        相关资源
        最近更新 更多