kasavbere
kasavbere

Reputation: 6003

JPA's EntityManager or Hibernate's HibernateTemplate with Spring

I have decided to use Spring, Hibernate, and Restlet to create a web service. I am new to all of these three technologies. My question is this: How do I decide whether to use JPA's EntityManager or Hibernate's HibernateTemplate?

Code snippet for JPA's EntityManager:

protected EntityManager entityManager;

@PersistenceContext
public void setEngityManager(EntityManager entityManger) {
    this.entityManager = entityManager;
}

Code snippet for Hibernate's HibernateTemplate:

private SessionFactory sessionFactory;
private HibernateTemplate hibernateTemplate;

@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
    this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}

Upvotes: 1

Views: 3813

Answers (2)

Bhagwati Malav
Bhagwati Malav

Reputation: 3549

If you want to stick to hibernate in future, then no point in using EntityManagerFactory, you can go ahead and use SessionFactory. And just inject definition for this using java based configuration or xml. It must be available in application context. But in future if you might want to move to different jpa provider, like toplink etc, then you should use EntityManagerFactory as it allows you to have implementation from various provider. Because providers implement jpa specification. So in future you just need to have different configuration in your application context and you should be able to use that.

Upvotes: 0

Vikdor
Vikdor

Reputation: 24124

If you are someone who like interface-based-implementation, then JPA is the interface definition and Hibernate is one of the implementations. We decided to use JPA semantics in our project (with a very long term plan to replace Hibernate with something similar and light-weight probably).

Upvotes: 2

Related Questions