118

I have

private EntityManager em;

public List getAll(DetachedCriteria detachedCriteria)   {

    return detachedCriteria.getExecutableCriteria("....").list();
}

How can I retrieve the session if am using entitymanager, or how can I get the result from my detached criteria?

peterh
  • 1
  • 15
  • 76
  • 99
storm_buster
  • 7,074
  • 18
  • 50
  • 71

5 Answers5

195

To be totally exhaustive, things are different if you're using a JPA 1.0 or a JPA 2.0 implementation.

JPA 1.0

With JPA 1.0, you'd have to use EntityManager#getDelegate(). But keep in mind that the result of this method is implementation specific i.e. non portable from application server using Hibernate to the other. For example with JBoss you would do:

org.hibernate.Session session = (Session) manager.getDelegate();

But with GlassFish, you'd have to do:

org.hibernate.Session session = ((org.hibernate.ejb.EntityManagerImpl) em.getDelegate()).getSession(); 

I agree, that's horrible, and the spec is to blame here (not clear enough).

JPA 2.0

With JPA 2.0, there is a new (and much better) EntityManager#unwrap(Class<T>) method that is to be preferred over EntityManager#getDelegate() for new applications.

So with Hibernate as JPA 2.0 implementation (see 3.15. Native Hibernate API), you would do:

Session session = entityManager.unwrap(Session.class);
Pascal Thivent
  • 549,808
  • 132
  • 1,049
  • 1,115
  • 1
    `entityManager.unwrap(Session.class);` what is `Session` in `Session.class`? is it an import? – Thang Pham Jan 13 '11 at 09:19
  • 1
    Depends on JPA implementation, if you're using eclipselink it's `org.eclipse.persistence.sessions.Session` – albciff Nov 29 '19 at 23:13
  • I had to put `@Transactional` on a repository class using `entityManager.unwrap(Session.class)`. Otherwise, I'm getting `java.lang.IllegalStateException: No transactional EntityManager available`. – Dmitriy Korobskiy Dec 23 '20 at 21:48
45

See the section "5.1. Accessing Hibernate APIs from JPA" in the Hibernate ORM User Guide:

Session session = entityManager.unwrap(Session.class);
informatik01
  • 15,636
  • 10
  • 72
  • 102
Vladimir Ivanov
  • 42,109
  • 17
  • 76
  • 102
  • `entityManager.unwrap(Session.class);` what is `Session` in `Session.class`? is it an import? – Thang Pham Jan 13 '11 at 09:21
  • 2
    The Hibernate Manual changed. Point 15.8 no longer gives any information about obtaining a session. – Nicktar Aug 27 '15 at 09:14
  • 3
    As of Jan. 2019, Hibernate current (5.3.7) manual , §5.1, still states this as the way to obtain a reference to a Session object. – Alain BECKER Feb 06 '19 at 23:31
7

This will explain better.

EntityManager em = new JPAUtil().getEntityManager();
Session session = em.unwrap(Session.class);
Criteria c = session.createCriteria(Name.class);
Vadim Martynov
  • 7,049
  • 5
  • 29
  • 39
Enio Dantas
  • 71
  • 1
  • 1
2

'entityManager.unwrap(Session.class)' is used to get session from EntityManager.

@Repository
@Transactional
public class EmployeeRepository {

  @PersistenceContext
  private EntityManager entityManager;

  public Session getSession() {
    Session session = entityManager.unwrap(Session.class);
    return session;
  }

  ......
  ......

}

Demo Application link.

Hari Krishna
  • 3,133
  • 1
  • 24
  • 47
-2

I was working in Wildfly but I was using

org.hibernate.Session session = ((org.hibernate.ejb.EntityManagerImpl) em.getDelegate()).getSession();

and the correct was

org.hibernate.Session session = (Session) manager.getDelegate();
brasofilo
  • 24,660
  • 15
  • 89
  • 174