users@glassfish.java.net

Re: Cast Exception while trying to getDelegate() from EntityManager

From: Sahoo <Sahoo_at_Sun.COM>
Date: Mon, 25 May 2009 13:02:32 +0530

You should be able to retrieve the session like this:

((EntityManagerImpl) em.getDelegate()).getSession()


The spec is horrible. EntityManager.getDelegate() is highly
implementation specific, so be careful.

Thanks,
Sahoo

glassfish_at_javadesktop.org wrote:
> 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
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>