I have an application which was using Hibernate 4.3.11.Final version earlier, and I decided to upgrade the Hibernate version into 5.6.3.Final.
After changing the pom dependencies, once I tried to login/run the application I'm repeatedly getting java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long .
The same query was running fine in previous Hibernate-4 version and returning correct result-sets, but now it's throwing exception after upgrading it to Hibernate-5.
I am wondering is there any datatype-differences between these 2 versions. How to fix this exception. Also I am using MySQL for database connectivity and jdk-8.
Below is the snap of pom.xml that I have right now:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.3.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>5.6.3.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
Code snippet is as follows:
String sqlQuery = "Select user_id, appl_id, user_name from User where delete_flag = 'N'";
NativeQuery sqlQry = hibernateSession.createSQLQuery(sqlQuery);
List<ObjectName> resultList = sqlQry.list();
And here is the entire exception log below:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
at org.hibernate.type.descriptor.java.LongTypeDescriptor.unwrap(LongTypeDescriptor.java:19) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
at org.hibernate.type.descriptor.sql.BigIntTypeDescriptor$1.doBind(BigIntTypeDescriptor.java:46) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:73) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:276) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:271) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
at org.hibernate.loader.custom.sql.NamedParamBinder.bind(NamedParamBinder.java:34) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
at org.hibernate.loader.custom.CustomLoader.bindParameterValues(CustomLoader.java:475) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
at org.hibernate.loader.Loader.bindPreparedStatement(Loader.java:2150) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:2127) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:2059) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:2037) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
at org.hibernate.loader.Loader.doQuery(Loader.java:956) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:357) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
at org.hibernate.loader.Loader.doList(Loader.java:2868) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
at org.hibernate.loader.Loader.doList(Loader.java:2850) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2682) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
at org.hibernate.loader.Loader.list(Loader.java:2677) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:338) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:2181) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
at org.hibernate.internal.AbstractSharedSessionContract.list(AbstractSharedSessionContract.java:1204) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
at org.hibernate.query.internal.NativeQueryImpl.doList(NativeQueryImpl.java:177) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1617) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
CodePudding user response:
- Maybe it is because of int or Integer, try replacing int with Integer or long with Long
- Or maybe possible that you are getting a long digit value that is going to be stored in the int type variable
CodePudding user response:
The preferred solution I got for this particular issue was, to upgrade the Hibernate version upto Hibernate-5.1.0.Final , which perhaps the highest working version in this current project environment.
The other small additional change you need to make for this was, to convert hibernateSession.getTransaction().isActive() into hibernateSession.getTransaction().getStatus() == TransactionStatus.ACTIVE . Otherwise you will get compilation errors.
