-->
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.  [ 6 posts ] 
Author Message
 Post subject: ClassCastException on EntityManager.getDelegate call
PostPosted: Tue Jul 15, 2008 5:12 am 
Beginner
Beginner

Joined: Mon Jan 10, 2005 7:14 am
Posts: 32
Hi,

I have a ClassCastException on call to getDelegate method of EntityManager class. My code is as following :

Code:
protected javax.persistence.EntityManager emanager;

protected org.hibernate.ejb.HibernateEntityManager hibernateSession;

@javax.persistence.PersistenceContext(unitName = "t4Seam")   
public void setEntityManager(javax.persistence.EntityManager entityManager) {
    this.emanager = entityManager;
    this.hibernateSession = (org.hibernate.ejb.HibernateEntityManager) entityManager.getDelegate();
}


And the stack trace :

Code:
Caused by: java.lang.ClassCastException: org.hibernate.impl.SessionImpl
   at t4.core.user.internal.UserAccountDaoBase.setEntityManager(UserAccountDaoBase.java:41)
   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:585)
   at org.jboss.injection.JndiMethodInjector.inject(JndiMethodInjector.java:94)
   at org.jboss.injection.JndiMethodInjector.inject(JndiMethodInjector.java:84)
   at org.jboss.injection.JndiMethodInjector.inject(JndiMethodInjector.java:58)
   at org.jboss.ejb3.AbstractPool.create(AbstractPool.java:111)
   at org.jboss.ejb3.InfinitePool.get(InfinitePool.java:49)
   at org.jboss.ejb3.ThreadlocalPool.create(ThreadlocalPool.java:50)
   at org.jboss.ejb3.ThreadlocalPool.get(ThreadlocalPool.java:90)
   at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:54)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
   at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:110)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:46)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:240)
   at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:210)
   at org.jboss.ejb3.stateless.StatelessLocalProxy.invoke(StatelessLocalProxy.java:84)
   at $Proxy306.findByUsername(Unknown Source)
   at t4.core.user.internal.UserInternalServiceBean.handleLoadUserAccount(UserInternalServiceBean.java:48)
   at t4.core.user.internal.UserInternalServiceBase.loadUserAccount(UserInternalServiceBase.java:97)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at


What is strange is that this piece of code is OK on unit tests running with OpenEJB. The problem occurs when running on JBoss AS only.

I thought it could come from conflicts between Hibernate jars bundled in JBoss AS and the ones in my EAR (hibernate-3.2.0.GA, hibernate-annotations-3.2.0.GA and hibernate-entitymanager-3.2.0.GA), but removing them from lib JBoss directory changed nothing.

Anyone encoutered a similar problem ?

Olivier


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 15, 2008 5:16 am 
Beginner
Beginner

Joined: Mon Jan 10, 2005 7:14 am
Posts: 32
Sorry for multiposts, I had an error after submitting my post.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 15, 2008 5:27 am 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
Hi,

Try the following

Code:
session = (Session)((EntityManagerImpl)mEntityManager.getDelegate()).getDelegate();


cheers,

Andy

_________________
Rules are only there to be broken


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 15, 2008 6:11 am 
Beginner
Beginner

Joined: Mon Jan 10, 2005 7:14 am
Posts: 32
Thanks for your help Andy.

Anyway it still doesn't work on JBoss AS. Still the same class cast exception when casting entityManager.getDelegate().

I debugged my piece of code, and entityManager is of org.jboss.ejb3.entity.TransactionScopedEntityManager type. So calling getHibernateSession() would work on it, but I guess this will not work with other application servers and this is an important point for my project. I will ask on JBoss forums to have help about this.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 15, 2008 6:22 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

I think your cast is wrong. You should cast to org.hibernate.Session.
The implementation in AbstractEntityManagerImpl is:

Code:
   public Object getDelegate() {
      return getSession();
   }


--Hardy

P.S. I deleted the duplicated posts.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 15, 2008 8:59 am 
Beginner
Beginner

Joined: Mon Jan 10, 2005 7:14 am
Posts: 32
Yes, you are right, it works on JBoss AS ... but it doesn't work on Open EJB !
Actually, with Open EJB, getDelegate method returns Hibernate entity manager. With JBoss AS, it returns Hibernate session ! Don't know which one does it the right way! Anyway, I find more logical to get entity manager rather than session. Anyone has an idea about this ?

Since I need my code to run on both application servers, I wrote it this way :

Code:
protected javax.persistence.EntityManager emanager;

protected org.hibernate.Session hibernateSession;

@javax.persistence.PersistenceContext(unitName = "t4Seam")
public void setEntityManager(javax.persistence.EntityManager entityManager) {
    this.emanager = entityManager;
    if (entityManager.getDelegate() instanceof org.hibernate.ejb.HibernateEntityManager) {
        this.hibernateSession = ((org.hibernate.ejb.HibernateEntityManager) entityManager.getDelegate()).getSession();
    }
    else {
       this.hibernateSession = (org.hibernate.Session) entityManager.getDelegate();
    }
}


Hope it will help.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.