Ayman R'bati Ayman R'bati - 3 months ago 24
Java Question

java.lang.Integer cannot be cast to java.lang.String

hi guys i m trying to make a query for a classic mapping class
they said java.lang.Integer cannot be cast to java.lang.String
but not sure wich string and wich int they are talking about

query :

@SuppressWarnings("unchecked")
public List<Champ> selectAll(int i){
String hql = "from Champ c WHERE c.etapesnum=:num";
List<Champ> lesChamps = factory.getCurrentSession().createQuery(hql)
.setParameter("num", 1).getResultList();

return lesChamps;
}


test code :

package DaoTest;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import Dao.*;
import mapping.*;
import Gestion.*;
public class yo {

public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Gestion.GestionChamp a=(Gestion.GestionChamp) context.getBean("gestionChamp",Gestion.GestionChamp.class);
for(Champ c:a.selectAll(1)){
System.out.println(c);
}


context.close();
}

}


Output :

août 14, 2016 2:56:33 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFOS: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@636be97c: startup date [Sun Aug 14 02:56:33 CEST 2016]; root of context hierarchy
août 14, 2016 2:56:33 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFOS: Loading XML bean definitions from class path resource [applicationContext.xml]
août 14, 2016 2:56:34 AM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
INFOS: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
août 14, 2016 2:56:34 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.1.Final}
août 14, 2016 2:56:34 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
août 14, 2016 2:56:34 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
août 14, 2016 2:56:34 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
août 14, 2016 2:56:36 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
août 14, 2016 2:56:39 AM org.springframework.orm.hibernate5.HibernateTransactionManager afterPropertiesSet
INFOS: Using DataSource [org.apache.commons.dbcp.BasicDataSource@7f284218] of Hibernate SessionFactory for HibernateTransactionManager
août 14, 2016 2:56:39 AM org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: select champ0_.id_champ as id_champ1_1_, champ0_.id_action as id_actio2_1_, champ0_.contexte as contexte3_1_, champ0_.num_etape as num_etap4_1_, champ0_.id_menu as id_menu5_1_, champ0_.id_selecteur as id_selec6_1_, champ0_.valeur_selecteur as valeur_s7_1_, champ0_.texte as texte8_1_ from champ champ0_ where champ0_.num_etape=?
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at org.hibernate.type.descriptor.java.StringTypeDescriptor.unwrap(StringTypeDescriptor.java:22)
at org.hibernate.type.descriptor.sql.VarcharTypeDescriptor$1.doBind(VarcharTypeDescriptor.java:46)
at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:74)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:277)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:272)
at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:53)
at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:628)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1950)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1903)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1881)
at org.hibernate.loader.Loader.doQuery(Loader.java:926)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:343)
at org.hibernate.loader.Loader.doList(Loader.java:2609)
at org.hibernate.loader.Loader.doList(Loader.java:2592)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2424)
at org.hibernate.loader.Loader.list(Loader.java:2419)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:502)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:371)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1450)
at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1402)
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1374)
at org.hibernate.Query.getResultList(Query.java:417)
at Dao.ChampDao.selectAll(ChampDao.java:38)
at Dao.ChampDao$$FastClassBySpringCGLIB$$5f2c82ef.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:280)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
at Dao.ChampDao$$EnhancerBySpringCGLIB$$82d88975.selectAll(<generated>)
at Gestion.GestionChamp.selectAll(GestionChamp.java:33)
at Gestion.GestionChamp$$FastClassBySpringCGLIB$$b7afa6fb.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:280)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
at Gestion.GestionChamp$$EnhancerBySpringCGLIB$$40c2dd19.selectAll(<generated>)
at automation.Test.main(Test.java:16)

Answer

Your problem is here:

.setParameter("num", 1).getResultList();

try:

 .setParameter("num", "1").getResultList();

Convert the int to a string and call setParameter, or better use setInteger().

I'm a little surprised setParaneter() doesn't handle the conversion. Oh the mysteries of strongly typed languages!

Comments