MySQL BIGINT conversion to Java Long

放肆的年华 提交于 2019-12-11 06:06:18

问题


Why a MYSQL column of type bigint cannot be casted to Java Long?

When using JPA.em().createNativeQuery("select ...").getResultList() and the select result is a column from the type bigint, you can't assign the result to a Long variable.

The cast causes a run time exception "java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long"

This of course can be solved by using a bigInteger variable and then ".longValue()"

I'm trying to understand what is the underlying reason for this error.

Both of them, Long and bigint, are signed 8 byte.

(for mySql: https://dev.mysql.com/doc/refman/5.5/en/integer-types.html)

(for java: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html)

According to different sources, Long is the equivalent java datatype for MYSQL bigint. (http://openwritings.net/content/public/java/mapping-java-and-mysql-data-types),

what distinguishes one from the other and makes the cast impossible?

Using Java 7 and MySql 5.5.57

来源:https://stackoverflow.com/questions/46998896/mysql-bigint-conversion-to-java-long

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!