【问题标题】:incompatible types: numeric and bigint. In PostgreSQL不兼容的类型:numeric 和 bigint。在 PostgreSQL 中
【发布时间】:2017-12-24 08:26:31
【问题描述】:

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

键列 veh_reg_authorityid 的类型不兼容:numericbigint。 以下是映射:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "VEH_REG_AUTHORITY", columnDefinition ="Numeric(12,0)")
private VehicleRegistrationAuthority vehicleRegistrationAuthority;

VehicleRegistrationAuthority 具有 id 类型的 Long,因此根据 PostgreSQL 转换为 BIGINT

所以一个简单的解决方案是相应地更改 columnDefinition ="Numeric(12,0)",但我们不能这样做,因为它适用于 oracle 和 mysql,并且它在 out 代码中被大量使用,因此不可能改变。

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

column                          java type             Oracle     Mysql   PostgreSQL
id                               Long                 NUMBER     BIGINT  BIGINT
veh_reg_authority VehicleRegistrationAuthority        NUMBER   decimal(12,0) NUMERIC

创建外键休眠时会出现以下问题。

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.

【问题讨论】:

    标签: sql oracle postgresql hibernate jpa


    【解决方案1】:

    如果你想定义外键约束,你应该为两列使用相同的数据类型。

    如果您的应用程序在 Oracle 中对两列都使用数据类型 NUMBER 可以正常工作,那么如果您对这两个列都使用 numeric,为什么它不能在 PostgreSQL 中工作?这些数据类型具有可比性。

    【讨论】:

    • 实际上,当您使用 Long(java 类型)时,Hibernate 将其映射到 java.sql.Types.BIGINT(JDBC type ),如果在 postgresql 中映射到 BIGINT。对于某些特定的基于精度的数字,我们必须使用 Numeric。 Numeric 和 BIGINT 在 MYSQL 中是兼容的,Oracle 对 BIGINT 和 NUMERIC 都使用 NUMBER,因此在 mysql 和 oracle 上没有问题。
    • 您应该为两列使用相同的 Java 类型,否则一切都是错误的。然后问题就消失了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-01
    • 1970-01-01
    相关资源
    最近更新 更多