不兼容的类型:numeric和bigint。在PostgreSQL中

不兼容的类型:numeric和bigint。在PostgreSQL中,sql,oracle,postgresql,hibernate,jpa,Sql,Oracle,Postgresql,Hibernate,Jpa,在迁移到postgresql时,我面临以下问题 键列veh\u reg\u authority和id的类型不兼容:numeric和bigint。 以下是映射: @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "VEH_REG_AUTHORITY", columnDefinition ="Numeric(12,0)") private VehicleRegistrationAuthority vehicleRegistrationAuth

在迁移到postgresql时,我面临以下问题

键列
veh\u reg\u authority
id
的类型不兼容:
numeric
bigint
。 以下是映射:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "VEH_REG_AUTHORITY", columnDefinition ="Numeric(12,0)")
private VehicleRegistrationAuthority vehicleRegistrationAuthority;
VehiclerRegistrationAuthority
具有
id
类型的
Long
,因此根据PostgreSQL将其转换为
BIGINT

因此,一个简单的解决方案是相应地更改columnDefinition=“Numeric(12,0)”,但我们无法做到这一点,因为它与oracle和mysql配合良好,而且它在代码中大量使用,因此无法更改

以下是Oracle、Mysql和PostgreSQL中的类型:

column                          java type             Oracle     Mysql   PostgreSQL
id                               Long                 NUMBER     BIGINT  BIGINT
veh_reg_authority VehicleRegistrationAuthority        NUMBER   decimal(12,0) NUMERIC
在创建外键时,hibernate给出了以下问题

Caused by: org.postgresql.util.PSQLException: ERROR: foreign key constraint "fk7gyqskn6x2l2910xhhtjgvh0j" cannot be implemented
Detail: Key columns "veh_reg_authority" and "id" are of incompatible types: numeric and bigint.

如果要定义外键约束,两列应使用相同的数据类型


如果您的应用程序在Oracle中对这两个列都使用数据类型
NUMBER
,那么如果您对这两个列都使用
numeric
,为什么不在PostgreSQL中工作呢?这些数据类型是可比较的。

实际上,当您使用Long(java类型)时,Hibernate会将其映射到java.sql.types.BIGINT(JDBC类型),如果在postgresql中映射到BIGINT,则会将其映射到java.sql.types.BIGINT。对于某些特定的基于精度的数字,我们必须使用数字。Numeric和BIGINT在MYSQL中是兼容的,Oracle对BIGINT和Numeric都使用NUMBER,因此MYSQL和Oracle没有问题。您应该对这两列使用相同的Java类型,其他所有内容都是错误的。然后问题就消失了。