JPA EntityManagerFactory Vs Hibernate’s SessionFactory
2017-03-16 20:24
513 查看
原文链接:http://javabeat.net/jpa-entitymanager-vs-hibernate-sessionfactory/?utm_source=tuicool&utm_medium=referral
If you are using the JPA’s standard specification implementation (Read :Introduction to JPA), then you would use EntityManagerFactory for
opening the session. But, if you are using the hibernate implementation, you have hibernate specific SessionFactory for managing the sessions. Here there is lot of confusion between developers like which one is the best
approach. Here, there is two opinions are popular:
如果你正在使用JPA的标准规范实现(阅读:介绍JPA),你会用EntityManagerFactory打开一个session。但是,如果您使用的是Hibernate规范,你将用hibernate特有的SessionFactory来管理session。很多开发者对于这二者哪一种是更好的实现很有疑惑,以下两点很普及:
EntityManagerFactory is the standard implementation, it is the same across all the implementations. If we migrate our ORM for any other provider, there will not be any change in the approach for handling the transaction.
In contrast, if you use hibernate’s session factory, it is tied to hibernate APIs and ca not migrate to new vendor easily.
One dis-advantage of using the standard implementation is that, it is not providing the advanced features. There is not much control provided in the EntityManager APIs. Whereas, hibernate’s SessionFactory has lot of advanced
features which can not done in JPA. One such thing is retrieving the ID generator without closing the transaction, batch insert, etc.
EntityManagerFactory 是一组标准,它在别的框架里不会发生改变。如果我们将我们的ORM迁移到其余开发者上,处理事务的方法不会有任何改变。相反的,如果你用hibernate的sessionfactory,它将用hibernate的api并且不能轻易的移植到其余的框架上。
2 使用这个标准的缺点之一是它不提供高级特性。在EntityManager API中不提供太多的控制。然而,hibernate的sessionFactory 有很多在JPA中不能实现的高级特性。比如在不关闭事务的情况下检索ID生成器,批处理插入等等。
Looking into the above points, one has to decide which one is better. There is no hard rule, after all it depends on the developers requirement. Another suggestion is that, we can use entity manger and session factory together. In this approach, entity manage
delegates session handling to the hibernate by invoking the unwrap method. Like this:
通过以上两点,我们很难说哪个更好。一切根据需求。我们可以将entity manager和sessionfactory同时使用。用这种方法,entitymanager通过调用unwrap方法来用hibernate的方法来实现
Using EntityManagerFactory approach allows us to use callback method annotations like @PrePersist, @PostPersist,@PreUpdate with no extra configuration. Using similar callbacks while using SessionFactory will
require extra efforts.
用EntityManagerFactory允许我们用回到方法的注解比如 @PrePersist, @PostPersist,@PreUpdate而不加额外配置。在sessionFactory中用相似的回调方法需要额外的配置。
If you are using the JPA’s standard specification implementation (Read :Introduction to JPA), then you would use EntityManagerFactory for
opening the session. But, if you are using the hibernate implementation, you have hibernate specific SessionFactory for managing the sessions. Here there is lot of confusion between developers like which one is the best
approach. Here, there is two opinions are popular:
如果你正在使用JPA的标准规范实现(阅读:介绍JPA),你会用EntityManagerFactory打开一个session。但是,如果您使用的是Hibernate规范,你将用hibernate特有的SessionFactory来管理session。很多开发者对于这二者哪一种是更好的实现很有疑惑,以下两点很普及:
EntityManagerFactory is the standard implementation, it is the same across all the implementations. If we migrate our ORM for any other provider, there will not be any change in the approach for handling the transaction.
In contrast, if you use hibernate’s session factory, it is tied to hibernate APIs and ca not migrate to new vendor easily.
One dis-advantage of using the standard implementation is that, it is not providing the advanced features. There is not much control provided in the EntityManager APIs. Whereas, hibernate’s SessionFactory has lot of advanced
features which can not done in JPA. One such thing is retrieving the ID generator without closing the transaction, batch insert, etc.
EntityManagerFactory 是一组标准,它在别的框架里不会发生改变。如果我们将我们的ORM迁移到其余开发者上,处理事务的方法不会有任何改变。相反的,如果你用hibernate的sessionfactory,它将用hibernate的api并且不能轻易的移植到其余的框架上。
2 使用这个标准的缺点之一是它不提供高级特性。在EntityManager API中不提供太多的控制。然而,hibernate的sessionFactory 有很多在JPA中不能实现的高级特性。比如在不关闭事务的情况下检索ID生成器,批处理插入等等。
Looking into the above points, one has to decide which one is better. There is no hard rule, after all it depends on the developers requirement. Another suggestion is that, we can use entity manger and session factory together. In this approach, entity manage
delegates session handling to the hibernate by invoking the unwrap method. Like this:
通过以上两点,我们很难说哪个更好。一切根据需求。我们可以将entity manager和sessionfactory同时使用。用这种方法,entitymanager通过调用unwrap方法来用hibernate的方法来实现
1 | Session session = entityManager.unwrap(Session. class ); |
require extra efforts.
用EntityManagerFactory允许我们用回到方法的注解比如 @PrePersist, @PostPersist,@PreUpdate而不加额外配置。在sessionFactory中用相似的回调方法需要额外的配置。
相关文章推荐
- JPA EntityManagerFactory Vs Hibernate' SessionFactory
- JPA EntityManagerFactory Vs Hibernate’s SessionFactory
- JPA:LocalContainerEntityManagerFactoryBean与LocalEntityManagerFactoryBean的区别
- hibernate对jpa的支持,自己实现AnnotationSessionFactoryBean,可以对jpa标签进行识别
- JPA+Spring下,applicationContext.xml中的entityManagerFactory中的class设置问题
- 在Spring环境中建立JPA Spring JPA 提供了三种方法创建JPA EntityManagerFactory:
- spring data jpa懒加载session失效:OpenEntityManagerInViewFilter 取代OpenSessionInViewFilter
- JPA EntityManager 获取session
- spring data jap的openEntityManagerInViewFilter和hibernate的openSessionInViewFilter
- JPA+Spring下,applicationContext.xml中的entityManagerFactory中的class设置问题
- JPA 实体管理器工厂(EntityManagerFactory)
- Hibernate SessionFactory openSession vs getCurrentSession vs openStatelessSession
- JPA报错, PersistenceException_Unable to build Hibernate SessionFactory
- JPA entityManagerFactory配置详解
- Source Taste: Hibernate/JPA: AbstractEntityManagerImpl
- spring data jpa 中的OpenEntityManagerInViewFilter 取代OpenSessionInViewFilter 放置session失效
- Hibernate 5的启动过程分析之EntityManagerFactory
- JPA与Spring2.5整合时发生不能创建entityManagerFactory的问题解决方法
- JPA--Caused by: javax.persistence.PersistenceException: [PersistenceUnit: mysqlJPA] Unable to configure EntityManagerFactory
- JPA EntityManager获得session、connection