Reputation: 6813
i have a doubt on concept i think i am mixing some stuff. I have a application with DAO layer the implementation of those DAO use SessionFactory (Hibernate) to execute the CRUD operation, i also have a service layer and manage my unit of work with Spring Declarative Transaction, and everything work fine, i am not sure about wether to use EntityManager or not but it seems that what it does is manage my unit of work (same thing that i am doing with Spring).
When should i use EntityManager? What is the advantages of using it? Why Should i use it?
Thank you.
Upvotes: 0
Views: 991
Reputation: 80166
EntityManager is for JPA what SessionFactory is for Hibernate. Spring can still manage your transaction even if you are uing JPA. If your JPA provider is Hibernate then you can mix and match Hibernate and JPA stuff safely as they both work together.
Now coming to the question of When to use EntityManager and the advantages:
If you are starting a new project and if you can use JPA 2 then I suggest you start using it as JPA is the spec (means that if you stick to the spec then you can change the implementations without changing the code. Eclipseling, DataNucleus, OpenJPA are some example implementations of JPA spec). If you can't move to JPA 2 then I suggest you stick to Hibernate.
Also go through the below posts
What does Hibernate/Toplink offer above JPA?
JPA or Hibernate for Java Persistence?
Upvotes: 2