Sunday, 26 August 2012

Retrieving Hibernate Session from JPA Application

The equivalent of JPA's EntityManager in Hibernate is a Session. The following describes how to retrieve an Hibernate Session (and SessionFactory) from a JPA configured application:
public class RetrievingHibernateSessionExample {

    private static EntityManagerFactory EMF;
    private static EntityManager EM;

    public static void main(String[] args) {

        // Creating resources
        EMF = Persistence.createEntityManagerFactory("Standalone");
        EM = EMF.createEntityManager();

        Object o = EM.getDelegate();
        System.out.println(o.getClass());

        Session s = (Session) o;

        SessionFactory sf = s.getSessionFactory();

}
This solution was suggested by Lando on Stackoverflow.com, with a link to an informative post about co-existence between Hibernate, JPA, and EJB3.


No comments:

Post a Comment