ClassCastException cannot be cast to java util Map
Hibernate: java.lang.ClassCastException: com.wfs.otc.datamodels.ImagineExpiryModel cannot be cast to java.util.Map
Problem: During working with Hibernate while trying to fetch and map data into Java POJO you get below exception:
java.lang.ClassCastException: com.wfs.otc.datamodels.ImagineExpiryModel cannot be cast to java.util.Map at org.hibernate.property.access.internal.PropertyAccessMapImpl$SetterImpl.set(PropertyAccessMapImpl.java:102) ~[hibernate-core-5.0.5.Final.jar:5.0.5.Final] at org.hibernate.transform.AliasToBeanResultTransformer.transformTuple(AliasToBeanResultTransformer.java:78) ~[hibernate-core-5.0.5.Final.jar:5.0.5.Final] at org.hibernate.hql.internal.HolderInstantiator.instantiate(HolderInstantiator.java:75) ~[hibernate-core-5.0.5.Final.jar:5.0.5.Final] at org.hibernate.loader.custom.CustomLoader.getResultList(CustomLoader.java:435) ~[hibernate-core-5.0.5.Final.jar:5.0.5.Final] at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2423) ~[hibernate-core-5.0.5.Final.jar:5.0.5.Final] at org.hibernate.loader.Loader.list(Loader.java:2418) ~[hibernate-core-5.0.5.Final.jar:5.0.5.Final] at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:336) ~[hibernate-core-5.0.5.Final.jar:5.0.5.Final] at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1967) ~[hibernate-core-5.0.5.Final.jar:5.0.5.Final] at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:322) ~[hibernate-core-5.0.5.Final.jar:5.0.5.Final] at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:125) ~[hibernate-core-5.0.5.Final.jar:5.0.5.Final] at com.wfs.otc.orm.dao.StoredProcDaoHandler.getImagineExpiryDetails(StoredProcDaoHandler.java:233) ~[classes/:?] at com.wfs.otc.orm.dao.StoredProcDaoHandler$FastClassBySpringCGLIB$a4002b98.invoke(<generated>) ~[classes/:?] at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:718) ~[spring-aop-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281) ~[spring-tx-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:654) ~[spring-aop-4.2.3.RELEASE.jar:4.2.3.RELEASE] at com.wfs.otc.orm.dao.StoredProcDaoHandler$EnhancerBySpringCGLIB$e95ec854.getImagineExpiryDetails(<generated>) ~[classes/:?] at com.wfs.otc.lifecyclemanager.LifeCycleManagerApp.main(LifeCycleManagerApp.java:28) ~[classes/:?]
Solution: This exception happens if column name return by the store procedure and SQL query is not exactly not matching with the Java model class. In my case Java model attribute name was different than column name return be the store procedure. To fix this issue I had to match Java model name with SQL column name. But it took me little longer to figure this out because exception details from hibernate above is very misleading.
This is not the reason, everything is fine and it doesn’t work!