如何使用Hibernate将Postgres _INT8映射到Java实体?

时间:2013-07-19 14:05:32

标签: java hibernate postgresql jpa orm

有一个使用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类型?

1 个答案:

答案 0 :(得分:3)

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

我不知道为什么使用下划线前缀,这很糟糕,但它应该只在服务器内可见,所以我很惊讶你看到它出现在消息中。例如,服务器显示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 forums。它seems to be a bit of an FAQ,但是像Hibernate / JPA中的大多数东西一样,你会发现只要你尝试使用除了最基本的数据库功能之外的任何东西,你就会碰到一堵砖墙。