users@glassfish.java.net

Cast Exception while trying to getDelegate() from EntityManager

From: <glassfish_at_javadesktop.org>
Date: Sun, 24 May 2009 10:50:28 PDT

Hello,

I'm using Glassfish 2.1 and Hibernate as my persistence provider. I'm using persistence.xml, and my persistence context is injected by @PersistenceContext. But when I try to reach Session object of Hibernate with getDelegate as follows:

@PersistenceContext
EnitityManager em;
 ...

Session session = (Session) em.getDelegate();

I get the exception at the bottom of the message. My work around is an ugly one:

Session session = (Session)((EntityManagerImpl) em.getDelegate()).getDelegate();

 which if possible I want to avoid, or if not, learn if it has any side effect?

Thanks

Caused by: java.lang.ClassCastException: org.hibernate.ejb.EntityManagerImpl cannot be cast to org.hibernate.Session
        at net.snodd.web.biz.CommentBean.getComments(CommentBean.java:29)
        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 com.sun.enterprise.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1011)
        at com.sun.enterprise.security.SecurityUtil.invoke(SecurityUtil.java:175)
        at com.sun.ejb.containers.BaseContainer.invokeTargetBeanMethod(BaseContainer.java:2920)
        at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4011)
        at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:203)
        ... 17 more
[Message sent by forum member 'ziphyre' (ziphyre)]

http://forums.java.net/jive/thread.jspa?messageID=347468