How do you retrieve Hibernate’s Session from an Entity ?

Reading the EJB 3.0 documentation it should be simply obtained by casting the EntityManager instance. org.hibernate.Session session = (org.hibernate.Session)entityManager; However, there seems to be a problem with JBoss AS 5.0, because a ClassCastException is issued. You can use the the getDelegate() method of the entity manager to avoid this problem: org.hibernate.Session session = (org.hibernate.Session)entityManager.getDelegate();

How to access Hibernate objects from EJB 3.0 ?

JBoss recipe of the day Basically you need to cast objects to Hibernate types. For example: Getting Hibernate Session 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(); } Getting Hibernate Query You … Read more