Why EntityManager's getDelegate() method does not return EntityManagerImpl as underlying object in Hibernate?
<p>I am working on a migration of an application from Hibernate/OpenEJB/TomEE to Hibernate/JBoss and I have this exception at runtime: </p>

<pre><code>java.lang.ClassCastException: org.hibernate.internal.SessionImpl cannot be cast to org.hibernate.ejb.EntityManagerImpl
</code></pre>

<p>While running this code:</p>

<pre><code>...
import javax.persistence.EntityManager;
...
@PersistenceContext(...)
protected EntityManager em;
...
EntityManagerImpl hibernateEntityManagerImpl = (EntityManagerImpl)em.getDelegate();
Session sess = hibernateEntityManagerImpl.getSession();
...
</code></pre>

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

<p>NB: </p>

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

<p>Thanks for your help.</p>