将Hibernate版本从4.3.11.Final升级到5.6.3.Final导致ClassCastException:



我有一个使用Hibernate的应用程序4.3.11.Final版本更早,我决定将Hibernate版本升级到5.6.3.Final。在更改pom依赖项之后,一旦我尝试登录/运行应用程序,我就会反复得到java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long

同样的查询在以前的Hibernate-4版本中运行良好,并返回正确的结果集,但现在它在升级到Hibernate-5后抛出异常。

我想知道这两个版本之间有没有数据类型的差异。如何修复此异常。此外,我使用MySQL进行数据库连接,并使用jdk-8

下面是我现在拥有的pom.xml的快照:

<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>

代码片段如下:

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();

下面是整个异常日志:

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]
  1. 可能是因为intInteger,请尝试将int替换为Integerong替代为long
  2. 或者,您可能得到一个的数字值,该值将存储在int类型变量中

针对这个特定问题,我得到的首选解决方案是将Hibernate版本升级到Hibernate-5.1.0.Final,这可能是当前项目环境中最高的工作版本。

您需要为此做的另一个小的额外更改是,将hibernateSession.getTransaction().isActive()转换为hibernateSession.getTransaction().getStatus() == TransactionStatus.ACTIVE。否则会出现编译错误。

相关内容

最新更新