Entity Manager is responsible for listener, entities, relationships, persist lifecycle of them and this interface defines the methods that are used to interact with the persistence context associated with an specific persistence context
TransactionManager is responsible for transactional data access, giving support to all the transaction that need to occurs within your application.
Transaction manager belongs with the Service layer, Entity Manager belong to the persistence layer
EntityManagerFactory and EntityManager are defined by the JPA standard. SessionFactory and Session are hibernate-specific. The EntityManager invokes the hibernate session under the hood
Why to prefer JPA API over the proprietary Hibernate?
The JPA API is cleaner than the Hibernate, Offers something that you can reuse in more other projects relying on a different implementation.
You can get the Hibernate Session from the JPA EntityManager
Session session = entityManager.unwrap(Session.class);
By using EntityManager, code is no longer tightly coupled with hibernate
JPA - javax.persistence.EntityManager
Hibernate - org.hibernate.ejb.HibernateEntityManager
Hibernate works better if your view is more object-centric. If however you view is more database-centric then Ibatis is a much stronger choice.
Hibernate is object-relation mapping framework (ORM) which maps Java classes to database tables. MyBatis is persistence framework – not ORM. It maps SQL statements to Java methods.
Hibernate has first level cache which is impossible to disable. It means that if you query item through ORM and then delete it directly with SQL, it stays in the cache. You can explicitly clear the cache to get the most updated results from database but unfortunately such behavior may bring errors like “detached entity passed to persist”
If you perform Create/Update/Delete of some complex domain entities Hibernate works well allowing you to just make a POJO and persist/update it. It also does this quickly, unless your domain is quite large.
Run analytic fetch queries (i.e. summation/aggregation queries). IBatis is great for fetch queries where you just want an answer.Hibernate would attempt to load the entire object graph and you’d need to start tuning queries with LazyLoading tricks to keep it working on a large domain. Conversely if you just want some analytic POJO page, the myBatis implementation of the same query would be trivial.