LocalContainerEntityManagerFactoryBean 和 LocalEntityManagerFactoryBean 有什么区别?
谁能解释一下Spring框架的LocalContainerEntityManagerFactoryBean和LocalEntityManagerFactoryBean之间有什么区别?
Can anybody explain what is the difference between the Spring Framework's LocalContainerEntityManagerFactoryBean and LocalEntityManagerFactoryBean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
基本上,JPA 规范定义了两种类型的实体管理器。他们是:
i) 应用程序管理:应用程序管理的实体管理器意味着“实体管理器仅由应用程序(即我们的代码)创建和管理”。
ii) 容器管理:容器管理实体管理器意味着“实体管理器仅由 J2EE 容器创建和管理(即我们的代码不直接管理,而是由容器创建和管理实体管理器,并且我们的实体管理器仅由 J2EE 容器创建和管理)”代码通过某种方式获取 EM,例如使用 JNDI )
注意:创建和管理(上面)意味着“在事务中打开、关闭和涉及实体管理器”
LocalContainerEntityManagerFactoryBean - 容器管理
LocalEntityManagerFactoryBean - 应用程序管理
重要说明:对于基于 Spring 的应用程序,差异不大。 Spring 仅扮演角色(如果您配置LocalContainerEntityManagerFactoryBean,则充当容器;如果您配置LocalEntityManagerFactoryBean<,则充当应用程序 /em>)
Basically JPA specification defines two types of entity managers. They are :
i) Application-Managed : Application Managed entity manager means "Entity Managers are created and managed by merely the application ( i.e. our code )" .
ii) Container Managed : Container Managed entity manager means "Entity Managers are created and managed by merely the J2EE container ( i.e. our code doesn't directly manages instead entity managers are created and managed by container , and our code gets EM's through some way like using JNDI ).
Note : Created and Managed (above) means "opening , closing and involving entity manager in transactions"
LocalContainerEntityManagerFactoryBean - container managed
LocalEntityManagerFactoryBean - application managed
A Big Note : For spring based applications, the difference is not much. Spring only plays roles ( as container if you configure LocalContainerEntityManagerFactoryBean and as application if you configure LocalEntityManagerFactoryBean)
该文档说明了一切:
LocalContainerEntityManagerFactoryBean -- 来自链接:FactoryBean 根据 JPA 的标准容器引导契约创建 JPA EntityManagerFactory。
LocalEntityManagerFactoryBean --从链接:FactoryBean 根据 JPA 的标准独立引导合同创建 JPA EntityManagerFactory。
本质上,唯一的区别在于它们创建 JPA EntityManagerFactory 的方式。
The documentation says it all:
LocalContainerEntityManagerFactoryBean -- From the link: FactoryBean that creates a JPA EntityManagerFactory according to JPA's standard container bootstrap contract.
LocalEntityManagerFactoryBean -- From the link: FactoryBean that creates a JPA EntityManagerFactory according to JPA's standard standalone bootstrap contract.
Essentially, the only difference is in how they create the JPA
EntityManagerFactory
.LocalEntityManagerFactoryBean
是最简单且最受限制的。您不能引用现有的 JDBC
DataSource bean 定义并且不支持全局事务。
LocalContainerEntityManagerFactoryBean
是最强大的 JPA 设置
选项,允许在应用程序内进行灵活的本地配置。它支持链接到现有的 JDBC 数据源,支持本地和全局事务
REF:spring-framework-reference.pdf“Spring 3”
LocalEntityManagerFactoryBean
is the simplest and the most limited. You cannot refer to an existing JDBC
DataSource bean definition and no support for global transactions exists.
LocalContainerEntityManagerFactoryBean
is the most powerful JPA setup
option, allowing for flexible local configuration within the application. It supports links to an existing JDBC DataSource, supports both local and global transactions
REF: spring-framework-reference.pdf "Spring 3"
LocalEntityManagerFactoryBean 生成应用程序管理的 EntityManagerFactory。
LocalContainerEntityManagerFactoryBean 产生一个容器管理的
实体管理器工厂。
参考:春天在行动 - Craig Walls
LocalEntityManagerFactoryBean produces an application-managed EntityManagerFactory.
LocalContainerEntityManagerFactoryBean produces a container-managed
EntityManagerFactory.
Ref : Spring In Action - Craig Walls
LocalContainerEntityManagerFactoryBean 返回 EntityManagerFactory
参考自
org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl。
明确要求 Spring 使用 JTA。
LocalContainerEntityManagerFactoryBean 以编程方式提供
设置持久化单元(数据源&packageToScan),更多
灵活之处在于我们可以覆盖 persistence.xml 的位置
文件与 LocalEntityManagerFactoryBean 进行比较,其中我们必须使用预定义的名称 persistence.xml
如果两者都使用 resources_local 作为默认值,那么它并不表示 LocalContainerEntityManagerFactoryBean 使用容器管理的事务而其他使用应用程序管理的事务。
在依赖注入容器外部使用 JPA 时,开发人员需要以编程方式处理事务。如果在 Spring 依赖注入容器内使用 JPA,那么它可以由 Spring 容器处理。
使用 LocalContainerEntityManagerFactoryBean 的示例
LocalEntityManagerFactoryBean 错误
java.lang.IllegalStateException: 不允许在共享 EntityManager 上创建事务 - 使用 Spring 事务或 EJB CMT 代替 使用
LocalEntityManagerFactoryBean 的工作代码
Spring 管理的事务类似于容器管理对于 LocalEntityManagerFactoryBean。
两种实现都可以在容器管理的事务下使用,如果需要一些更正,请纠正我。
LocalContainerEntityManagerFactoryBean returns EntityManagerFactory
reference from
org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.
explicitly ask Spring to use JTA.
LocalContainerEntityManagerFactoryBean provide programmatically
setting persistence unit (data source & packageToScan), is more
flexible in that we can override the location of the persistence.xml
file compare to LocalEntityManagerFactoryBean in which we have to use a predefined name persistence.xml
If both are using resource_local as default then it does not thumb rule that LocalContainerEntityManagerFactoryBean is using container-managed transaction and other is using application-managed transaction.
When using JPA outside of a dependency injection container, transactions need to be handled programmatically by the developer. If using JPA inside of Spring dependency injection container then it can be handled by Spring container.
Example using LocalContainerEntityManagerFactoryBean
Error with LocalEntityManagerFactoryBean
java.lang.IllegalStateException: Not allowed to create a transaction on shared EntityManager - use Spring transactions or EJB CMT instead
Working code with LocalEntityManagerFactoryBean
Spring managed transaction like container-managed in case of LocalEntityManagerFactoryBean.
Both implementations can be used under container-managed transaction, please correct me on this if some correction is need.
要在 Spring 项目中使用 JPA,我们需要设置 EntityManager。
这是配置的主要部分,我们可以通过 Spring 工厂 bean 来完成。
这可以是更简单的 LocalEntityManagerFactoryBean 或更灵活的 LocalContainerEntityManagerFactoryBean。
baeldung.com/the-persistence-layer-with-spring-and-jpa
To use JPA in a Spring project, we need to set up the EntityManager.
This is the main part of the configuration, and we can do it via a Spring factory bean.
This can be either the simpler LocalEntityManagerFactoryBean or the more flexible LocalContainerEntityManagerFactoryBean.
baeldung.com/the-persistence-layer-with-spring-and-jpa
LocalEntityManagerFactoryBean 通过 PersistenceProvider.createEntityManagerFactory() 创建 EntityManagerFactory
LocalContainerEntityManagerFactoryBean 通过 PersistenceProvider.createContainterEntityManagerFactory() 创建 EntityManagerFactory
LocalEntityManagerFactoryBean creates EntityManagerFactory via PersistenceProvider.createEntityManagerFactory()
LocalContainerEntityManagerFactoryBean creates EntityManagerFactory via PersistenceProvider.createContainterEntityManagerFactory()