JPA与PostgreSQL 9.5的模式验证

6

我正在使用JPA(hibernate)与Postgres数据库,我的hibernate数据库配置如下:

properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
properties.setProperty("hibernate.hbm2ddl.auto","validate");

我有一张表格,其中包含属性created_by_user_id

CREATE TABLE dppm_main.user_account_profile
( ....//other columns
  created_by_user_id integer
)

以下是映射到JPA实体的内容:

@Column(name = "created_by_user_id")
private Long createdByUserId;

在进行模式验证时,我遇到了以下错误:

org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column 
type encountered in column [created_by_user_id] in table [user_account_profile];
 **found [int4 (Types#INTEGER)], but expecting [int8 (Types#BIGINT)]**

如何解决这个问题?我在很多地方都遇到了这个问题,是否可以通过扩展PostgreSQL82Dialect而不是columnDefinition来解决它?

1
我对JPA一无所知,但错误信息表明它期望该列被定义为bigint,因为你的JPA实体使用了Long。尝试在JPA实体中使用Integer或在表定义中使用bigint - user330315
1
@suraj兄弟,你解决了吗?我现在也遇到了同样的问题。 - Linitha
1个回答

10
该异常明显指出了Java类型和Postgres类型之间的类型不匹配。我不清楚您在Postgres中的类型是什么,因此我将指出一些类型转换:
Postgres: smallint to Java --> java.lang.Short
Postgres: integer to Java --> java.lang.Integer Postgres: bigint to Java --> java.lang.Long
话虽如此,只需更改以下内容:
@Column(name = "created_by_user_id")
private Long createdByUserId;

致:

@Column(name = "created_by_user_id")
private Integer createdByUserId;

5
Postgresдёӯзҡ„smallintзҝ»иҜ‘дёәJavaеә”иҜҘжҳҜshortгҖӮ - Андрей

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接