0

I'm learning web development and so far, have about , EJB, JSTL and JPA (Hibernate). I understand that SessionFactory object should be initialized only once in the project as Singleton. I'm confused how do I initialize this object. Where to keep this code which will initialize this object?

Bharat Nanwani
  • 653
  • 4
  • 11
  • 27

1 Answers1

0

You don't need to. At least, not in a real Java EE environment with EJB and JPA.

You seem to be mixing up "Legacy Hibernate" and JPA. Previously, when JPA didn't exist, there was Hibernate as the de facto ORM standard. This is "Legacy Hibernate", which you still see back in a lot of old or amateuristic tutorials these days, mainly in combination with Java EE's long time competitor Spring. You'll see old fashioned things like SessionFactory and Session only back in there.

Legacy J2EE users wanted to unify/standardize the "ORM" in Java EE API. The Hibernate guy Gavin King was hired to do all the JPA works. JPA was designed based on lessons learnt from Hibernate. Since JPA was added to Java EE 5 (in 2006 already!), Hibernate offered a JPA implementation too. JPA is like good ol' JDBC an abstract API, which anyone could provide an implementation for (in good ol' JDBC, those are called "JDBC drivers").

Hibernate is not the only JPA implementation, other JPA implementations are EclipseLink and OpenJPA. Full fledged Java EE containers already provide a JPA implementation out the box. JBoss WildFly provides Hibernate, while GlassFish provides EclipseLink and TomEE provides OpenJPA. You don't need to manually install a JPA implementation on those servers, but you could if necessary replace it.

The SessionFactory is part of Legacy Hibernate, not JPA. The SessionFactory is never shown in any of JPA tutorials. This can only mean that you're reading the wrong or heavily outdated resources to learn JPA. Perhaps you searched for those tutorials using keyword "Hibernate" while you should be searching for tutorials using keyword "JPA". The JPA equivalent of Legacy Hibernate's SessionFactory is EntityManagerFactory. And, the JPA equivalent of Legacy Hibernate's Session is EntityManager. You should be using those instead.

Provided a real Java EE environment with the persistence.xml set to transaction-type="JTA", all you need to do to get a hand of the EntityManager is simply injecting it via @PersistenceContext in an EJB class.

E.g., an imaginary service for a @Entity public class Product:

@Stateless
public class ProductService {

    @PersistenceContext
    private EntityManager em;

    public Product find(Long id) {
        return em.find(Product.class, id);
    }

    public List<Product> list() {
        return em.createQuery("SELECT p FROM Product p", Product.class).getResultList();
    }

    public void create(Product product) {
        em.persist(product);
    }

    public void update(Product product) {
        em.merge(product);
    }

    public void delete(Product product) {
        em.remove(em.contains(product) ? product : em.merge(product));
    }

}

That's all, really. No need to maintain EntityManagerFactory, nor to manually start/commit/rollback user transactions and such. The Java EE container will manage all by itself via EJB and JTA, saving you from repeating the same code boilerplate again and again.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you so much for this. I've been both the ways actually. To your surprise, I got comfortable with Legacy Hibernate but, I did find Session objects a bit strange because, I didn't any of these in the other set of JPA tutorials. – Bharat Nanwani Sep 19 '15 at 14:14
  • Another quick question, EntityManager can also be used in Hibernate huh? – Bharat Nanwani Sep 19 '15 at 14:15
  • If you use Hibernate's JPA implementation, yes. The concrete `EntityManager` instance is then under the covers offered by Hibernate. Look, it's one and all interfaces like JDBC. – BalusC Sep 19 '15 at 14:35