【问题标题】:Spring data JPA casting unsigned BITINT to Long instead of BigIntegerSpring data JPA 将 unsigned BIGINT 转换为 Long 而不是 BigInteger
【发布时间】:2019-12-03 18:11:13
【问题描述】:

我的数据库表中有一个 unsigned bigint(20) 列。

mysql driver version
8.0.14

我的 JPA 实体如下所示:

BigInteger columnName;

问题是当我尝试使用以下选择查询获取数据时:

select * from table where columnName in (large number [unsigned bigint])

我收到以下错误:

java.sql.SQLDataException: Value '13,224,435,352,132,323,241' is outside of valid range for type java.lang.Long
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:114)
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:92)
        at com.mysql.cj.jdbc.result.ResultSetImpl.getLong(ResultSetImpl.java:923)
        at com.mysql.cj.jdbc.result.ResultSetImpl.getLong(ResultSetImpl.java:928)

所以我看到 ResultSetImpl 的代码找到了这个

case BIGINT:
    return Long.valueOf(getLong(columnIndex));

case BIGINT_UNSIGNED:
    return getBigInteger(columnIndex);

但即使表中的数据类型是 unsigned bigint(20),它仍在调用 getLong()。

我看到的一种方法是编写自定义存储库并进行解析。

想问问大家有没有遇到过这个问题,如果有,你们是怎么解决的?

谢谢。

【问题讨论】:

  • 是否可以将数字(姓名?)存储为文本?

标签: java spring jpa spring-data mysql-8.0


【解决方案1】:

更新:我通过使用 jdbcTemplate 和 rowMapper 将有问题的列转换为 Biginteger/BigDecimal 解决了这个问题

【讨论】:

    【解决方案2】:

    您还没有为您的数据类型提供 DDL(数据定义语言),因此我无法确定您在数据库中的数据类型实际上是什么。您可以做的是使用 cast() 函数在您的查询中强制转换它。因此,您的查询将如下所示:

    select column, column, cast(bigint_unsigned_column as unsigned bigint), column
    from table where columnName in (large number [unsigned bigint])
    

    【讨论】:

    • 试过这个。还是一样的Value '13,224,435,352,132,323,241' is outside of valid range for type java.lang.Long
    猜你喜欢
    • 2015-09-09
    • 1970-01-01
    • 2016-09-03
    • 2012-07-20
    • 1970-01-01
    • 2016-12-20
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    相关资源
    最近更新 更多