-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: ClassCastException in Hibernate 4.3.6-Final
PostPosted: Fri Oct 10, 2014 4:37 pm 
Newbie

Joined: Fri Oct 10, 2014 4:24 pm
Posts: 2
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];
}

}


Top
 Profile  
 
 Post subject: Re: ClassCastException in Hibernate 4.3.6-Final
PostPosted: Mon Oct 13, 2014 8:28 am 
Newbie

Joined: Fri Oct 10, 2014 4:24 pm
Posts: 2
What I doing wrong?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.