Boa tarde Galera.
To com um problema, acontece o seguinte erro
Exception in thread “main” java.lang.ClassCastException: br.com.restorant.entity.Category cannot be cast to br.com.restorant.entity.Category
at br.com.restorant.dao.impl.CategoryDaoImpl.findById(CategoryDaoImpl.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:138)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy25.findById(Unknown Source)
at Execute.main(Execute.java:12)
assim a classe br.com.restorant.entity.Category é um pojo.
Eu estou usando Spring e fazendo uns testes e tá gerando o erro, mas a classe é a mesma e o java alega não poder fazer o cast.
public class Execute {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
CategoryDao categoryDao = (CategoryDao)context.getBean("CategoryDao");
System.out.println(categoryDao.findById(4));
}
}
o erro ocorre aqui
@Override
public Category findById(long id) {
Query query = em.createNamedQuery("Category.findById");
query.setParameter("id",id);
Category category = (Category)query.getSingleResult();
return category;
}
o erro ocorre na hora que ele faz o cast do resultado do getSingleResult();
estou usando jpa. Se eu der um system.out, motra que o resultado tá certo, alguém já passou por e sabe resolver