Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA schema validation with PostgreSQL 9.5

I am using JPA (hibernate) with Postgres database, my hibernate database configuration are as below:

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

I have a table with property created_by_user_id

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

Mapped to JPA entity as below:

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

While doing schema validation, I am getting following error:

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)]**

How can I fix it? I am having this issue in many places so is it possible to fix it by extending PostgreSQL82Dialect instead of columnDefinition.

like image 691
suraj bahl Avatar asked Oct 04 '16 15:10

suraj bahl


1 Answers

The exception is clearly speaking about types mismatch between Java types and Postgres types.

I didn't understand clearly what is your type in Postgres, so I'll point a few types conversions:

Postgres: smallint to Java --> java.lang.Short

Postgres: integer to Java --> java.lang.Integer

Postgres: bigint to Java --> java.lang.Long

With that being said, just change from:

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

To:

@Column(name = "created_by_user_id")
private Integer createdByUserId;
like image 117
Moshe Arad Avatar answered Oct 31 '22 10:10

Moshe Arad