I have a project works perfectly with Hibernate 4.3.5-Final but in Hibernate 4.3.6-Final occurred ClassCastException when Hibernate perform a query.
Caused by: java.lang.ClassCastException: com.pnp.teste.projetotestehibernate.Usuario cannot be cast to com.pnp.teste.projetotestehibernate.AbstractModel at com.pnp.teste.projetotestehibernate.GenericHibernateDao.findUniqueResult(GenericHibernateDao.java:191) at com.pnp.teste.projetotestehibernate.GenericHibernateDao.findUniqueResult(GenericHibernateDao.java:181) at com.pnp.teste.projetotestehibernate.UsuarioDaoImpl.buscarPorUsername(UsuarioDaoImpl.java:36) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081) at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153) at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4695) at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:630) at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822) at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582) at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:46) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883) at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822) at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369) at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4667) at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4655) at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212) ... 64 more
My pojo and my dao
@Entity @Table(name = "usuario") @SequenceGenerator(allocationSize = 1, name = "seq_usuario", sequenceName = "seq_usuario") public class Usuario extends AbstractModel { // some attributes }
@Stateful(name = "usuarioDao") @Named @UsuarioDaoQualifier @LocalBean public class UsuarioDaoImpl extends GenericHibernateDao<Usuario, Serializable> implements UsuarioDao, Serializable {
private static final long serialVersionUID = -2133771624984338700L;
@Override public Class<Usuario> getPersistentClass() { return Usuario.class; } }
public abstract class GenericHibernateDao<T extends AbstractModel, K extends Serializable> implements Serializable, GenericDao<T, K> {
private static final long serialVersionUID = -8773809458616814954L;
@PersistenceContext(type = PersistenceContextType.EXTENDED) protected EntityManager em; protected Session session;
public Session getSession() { if (session == null) { if (em.getDelegate() instanceof org.hibernate.ejb.HibernateEntityManager) { session = ((org.hibernate.ejb.HibernateEntityManager) em.getDelegate()).getSession(); } else { session = (org.hibernate.Session) em.getDelegate(); } } return session; }
@SuppressWarnings("unchecked") @Override public List<T> findAll() { Criteria criteria = getSession().createCriteria(this.getPersistentClass()); return criteria.list(); }
@SuppressWarnings({ "unchecked" }) @Override public T findOneByExample(final T entity) { Criteria criteria = getSession().createCriteria(this.getPersistentClass()); criteria.add(Example.create(entity).enableLike(MatchMode.ANYWHERE).ignoreCase()); return (T) criteria.uniqueResult(); }
@SuppressWarnings("unchecked") public Class<T> getPersistentClass() { return (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; }
}
|