MySQL: ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long – Fix

The problem seems with versions of MySQL in combination with old version of mysql-connector.jar. Just tried with below mysql-connector.jar version with MySQL 8.0 and seems to work for me perfectly.

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.4.4</version>
	<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<scope>runtime</scope>
	<version>8.0.23</version>
</dependency>

If you’re encountering a ClassCastException in a MySQL context where a java.math.BigInteger cannot be cast to java.lang.Long, it could be due to the types of columns or values being retrieved from the database.

MySQL uses BIGINT to represent large integer values, and the JDBC driver may return BigInteger for these values. If your code is expecting a Long and directly trying to cast, it can lead to a ClassCastException.