MySQL BIGINT conversion to Java Long

2019-08-05 10:26发布

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

0条回答
登录 后发表回答