【问题标题】:How to map Postgres _INT8 to Java entity using Hibernate?如何使用 Hibernate 将 Postgres _INT8 映射到 Java 实体?
【发布时间】:2013-07-18 20:37:51
【问题描述】:

有一个使用 SQL DDL 脚本创建的表,其中有一列类型为 _INT8。如果我尝试将其映射到 long(即 Postgres INT8),它会在堆栈的末尾抛出。

Caused by: org.hibernate.HibernateException: Wrong column type in [schme_name].[table_name] for column [column_name]. Found: _int8, expected: int8
    at org.hibernate.mapping.Table.validateColumns(Table.java:373)
    at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1265)
    at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:155)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:508)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1750)
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:94)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:920)

如果我尝试将其映射到 long[](或任何其他数组类型)而不是 Found: _int8, expected: bytea

如何使用 Hibernate 将 Postgres 的 _INT8 映射为 Java 类型?

【问题讨论】:

    标签: java hibernate postgresql jpa orm


    【解决方案1】:

    _int8int8[] 类型的内部别名,即长整数数组。

    我不知道为什么要使用下划线前缀,这很可怕,但它实际上应该只在服务器内部可见,所以我很惊讶你看到它出现在消息中。例如,服务器将bigint[] 显示为消息中的列类型:

    http://sqlfiddle.com/#!12/61bc5/1

    如果你想在 Hibernate 中映射它,你必须将它映射为 long[],如果 Hibernate 甚至支持 SQL 数组 - which it does not appear to。你可能不得不add your own UserType implementation that uses the JDBC support for SQL arraysAnother example on the Hibernate forumsseems to be a bit of an FAQ,但就像 Hibernate/JPA 中的大多数东西一样,您会发现,一旦您尝试使用除最基本的数据库功能之外的任何东西,您的头就会撞到砖墙上。

    【讨论】:

      猜你喜欢
      • 2015-02-24
      • 2017-11-02
      • 1970-01-01
      • 2018-09-08
      • 2018-12-10
      • 2015-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多