[Design of EJB 3.0] - Accessing org.hibernate.Session from EntityManager
by jaikiran
For accessing org.hibernate.Session from a EntityManager, the JBossAS4.x docs for EJB3, recommend:
anonymous wrote : You can get access to the current underlying Hibernate Session by typecasting your reference to EntityManager.
|
| @PersistenceContext EntityManager entityManager;
| | public void someMethod();
| | {
| | org.jboss.ejb3.entity.HibernateSession hs = (HibernateSession)entityManager;
| | org.hibernate.Session session = hs.getHibernateSession();
| | }
| |
However, in AS5, the cast throws an CCE:
| [java] Exception in thread "main" javax.ejb.EJBException: java.lang.ClassCastException: org.jboss.jpa.tx.TransactionScopedEntityManager
| [java] at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:77)
| [java] at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:83)
| [java] at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:190)
| [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
| [java] at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
| [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
| [java] at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
| [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
| [java] at org.jboss.ejb3.security.Ejb3AuthenticationInterceptorv2.invoke(Ejb3AuthenticationInterceptorv2.java:186)
| [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
| [java] at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:41)
| [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
| [java] at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
| [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
| [java] at org.jboss.ejb3.BlockContainerShutdownInterceptor.invoke(BlockContainerShutdownInterceptor.java:67)
| [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
| [java] at org.jboss.aspects.currentinvocation.CurrentInvocationInterceptor.invoke(CurrentInvocationInterceptor.java:67)
| [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
| [java] at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:487)
| [java] at org.jboss.ejb3.session.InvokableContextClassProxyHack._dynamicInvoke(InvokableContextClassProxyHack.java:53)
| [java] at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:91)
| [java] at org.jboss.aspects.remoting.AOPRemotingInvocationHandler.invoke(AOPRemotingInvocationHandler.java:82)
| [java] at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:908)
| [java] at org.jboss.remoting.transport.socket.ServerThread.completeInvocation(ServerThread.java:742)
| [java] at org.jboss.remoting.transport.socket.ServerThread.processInvocation(ServerThread.java:695)
| [java] at org.jboss.remoting.transport.socket.ServerThread.dorun(ServerThread.java:522)
| [java] at org.jboss.remoting.transport.socket.ServerThread.run(ServerThread.java:230)
| [java] Caused by: java.lang.ClassCastException: org.jboss.jpa.tx.TransactionScopedEntityManager
| [java] at org.jboss.tutorial.hibernate.bean.AnotherCustomerBean.getCustomer(AnotherCustomerBean.java:68)
|
for this line:
org.jboss.ejb3.entity.HibernateSession hs = (HibernateSession)entityManager;
>From the javadocs of javax.persistence.EntityManager, i see that there is a getDelegate method available which can be used instead. So i have changed the reference doc and also added a new tutorial with this piece of code, which is working;
public Customer getCustomer(long id)
| {
| org.hibernate.Session session = (Session) em.getDelegate();
| return (Customer) session.get(Customer.class, id);
| }
|
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4205769#4205769
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4205769
15 years, 3 months