我正在尝试将 SHA256 哈希存储为数值,这似乎是从空间和索引 POV 执行此操作的最佳方式。我正在使用具有简单字段定义的 JPA:
@NonNull
private BigInteger mybiginteger;
但是,它拒绝插入该值。
我在单元测试期间使用 H2 数据库,现在我在 JPA 中添加了一个 BigInteger 值,它失败了
Caused by: org.h2.jdbc.JdbcSQLDataException: Value too long for column "MYBIGINTEGER DECIMAL(19, 2)": "-12804752987762098394035772686106585063470084017442529046078187006797464553387.00 (79)"; SQL statement:
跳过测试用例并使用 Postgres 尝试它也失败了
Caused by: org.postgresql.util.PSQLException: ERROR: numeric field overflow
Detail: A field with precision 19, scale 2 must round to an absolute value less than 10^17.
JPA 根据 DB 工具将其创建为 numeric(19,2) 字段。我可以手动更改列定义并且它可以工作,但不能将其设置为在 H2 和 PSQL 之间可移植。
如何使用 JPA 将 BigInteger 正确存储在 H2/PSQL 数据库中?另外,为什么 BigInteger 类型允许保留两位小数?