0

I am working on a migration of an application from Hibernate/OpenEJB/TomEE to Hibernate/JBoss and I have this exception at runtime:

java.lang.ClassCastException: org.hibernate.internal.SessionImpl cannot be cast to org.hibernate.ejb.EntityManagerImpl

While running this code:

...
import javax.persistence.EntityManager;
...
@PersistenceContext(...)
protected EntityManager em;
...
EntityManagerImpl hibernateEntityManagerImpl = (EntityManagerImpl)em.getDelegate();
Session sess = hibernateEntityManagerImpl.getSession();
...

The same code is working in TomEE. I don't know why the getDelegate() method does not return an EntityManager as an underlying object.

NB:

  • The application is already with hibernate (with lawer version) before and I am just migrating it to JBoss;
  • I am using Hibernate 4.2.21.Final (same version for EntityManager);
  • persistence.xml provider is org.hibernate.ejb.HibernatePersistence

Thanks for your help.

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
Walid Ammou
  • 390
  • 1
  • 6
  • 21

3 Answers3

1

If you are working with JBoss EAP / Hibernate, you can replace

EntityManagerImpl hibernateEntityManagerImpl = (EntityManagerImpl)em.getDelegate();

By :

org.hibernate.Session sess = (Session) em.getDelegate();
Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
Kikou
  • 1,895
  • 4
  • 20
  • 32
  • I have already used your solution and it works :D. The better solution for new applications is `final org.hibernate.Session sess = this.em.unwrap(org.hibernate.Session.class); ` . But I want to understand why this exception is handled! Why it works on TomEE and not JBoss! – Walid Ammou Mar 15 '16 at 09:39
0

Please note that getDelegate method implementation is provider specific. What works for one provider will not necessarily work for another jpa provider. Here is what javadoc says about getDelegate()

Return the underlying provider object for the EntityManager, if available. The result of this method is implementation specific.

JPA Specification tells what is mandatory for a provider and what is not. "Not Mandatory" things in jpa specification will be or won't be implemented by jpa provider.

It is better to use only the mandatory specification in JPA as our goal is to reduce the dependency of provider from JPA. We should not use provider specific implementations.

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
  • It's amazing in the example of Walid, he uses the same provider Hibernate , so i can 't explain why it works on tomEE and not Jboss. Can you give me more information please – Kikou Mar 11 '16 at 10:12
0

You didn't tell which version of JBoss EAP you use, so I assume it's the latest.

JBoss EAP / JBoss AS / WildFly bundles it's own JBoss Hibernate. If you have another Hibernate library bundled in your application, it may or may not be used by your application's classloader, based on classloading configuration.

If your application is Maven-based, you should follow the JBoss Developer guides. Check the JBoss Quickstarts and rework your pom accordingly. It's really worth it and will save you a lot of problems.

Lastly, try to avoid using proprietary API. Every time you retype some interface to a particular implementation, you're potentially using a proprietary API and ask for troubles.

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277