spring EntityManager bean 的 @Autowired 与 @PersistenceContext
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31335211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
@Autowired vs @PersistenceContext for EntityManager bean
提问by Cosmin Vasii
What is the difference between:
有什么区别:
@Autowired
private EntityManager em;
versus:
相对:
@PersistenceContext
private EntityManager em;
Both options work in my application, but can I break something by using the @Autowired
annotation?
这两个选项都适用于我的应用程序,但是我可以通过使用@Autowired
注释来破坏某些东西 吗?
回答by sashok_bg
@PersistenceContext
allows you to specify which persistence unit you want to use. Your project might have multiple data sources connected to different DBs and @PersistenceContext
allows you to say which one you want to operate on
@PersistenceContext
允许您指定要使用的持久性单元。您的项目可能有多个数据源连接到不同的数据库,并@PersistenceContext
允许您说出要操作的数据源
check the explanation here: http://www.coderanch.com/t/481448/java-EJB-SCBCD/certification/unitName-PersistenceContext
检查这里的解释:http: //www.coderanch.com/t/481448/java-EJB-SCBCD/certification/unitName-PersistenceContext
回答by MagGGG
@PersistenceContext:
@PersistenceContext:
does notreturn entity manager instance
不返回实体管理器实例
it returnscontainer-managed proxy that acquires and releases presistence context on behalf of the application code
它返回代表应用程序代码获取和释放存在上下文的容器管理的代理
回答by Alex
@PersistenceContext
is a JPAstandard annotation designed for that specific purpose. Whereas @Autowired
is used for any dependency injection in Spring. Using @PersistenceContext
gives you greater control over your context as it provides you with ability to specify optional elements e.g. name, properties
@PersistenceContext
是专为该特定目的而设计的JPA标准注释。而@Autowired
用于 Spring 中的任何依赖注入。使用@PersistenceContext
使您可以更好地控制上下文,因为它使您能够指定可选元素,例如名称、属性
回答by Alex
You shouldn't use @Autowired
.
@PersistenceContext
takes care to create a unique EntityManager for every thread. In a production application you can have multiple clients calling your application in the same time. For each call, the application creates a thread. Each thread should use its own EntityManager. Imagine what would happen if they share the same EntityManager: different users would access the same entities.
你不应该使用@Autowired
.
@PersistenceContext
注意为每个线程创建一个唯一的 EntityManager。在生产应用程序中,您可以让多个客户端同时调用您的应用程序。对于每次调用,应用程序都会创建一个线程。每个线程都应该使用自己的 EntityManager。想象一下,如果他们共享同一个 EntityManager 会发生什么:不同的用户将访问相同的实体。
usually the EntityManager or Session are bound to the thread (implemented as a ThreadLocal variable).
Source: https://stackoverflow.com/a/42074452/2623162
来源:https: //stackoverflow.com/a/42074452/2623162
EntityManager instances are not thread-safe.
Source: https://docs.oracle.com/cd/E19798-01/821-1841/bnbqy/index.html
来源:https: //docs.oracle.com/cd/E19798-01/821-1841/bnbqy/index.html
Please notice that @PersistenceContext
annotation comes from javax.persistence
package, not from spring framework. In JavaEE it is used by the JavaEE container (aka the application server) to inject the EntityManager. Spring borrowed the PersistenceContext annotation to do the same: to inject an application-managed(= not container-managed) EntityManager bean per thread, exactly as the JavaEE container does.
请注意,@PersistenceContext
注解来自javax.persistence
包,而不是来自 spring 框架。在 JavaEE 中,它被 JavaEE 容器(也称为应用服务器)用来注入 EntityManager。Spring 借用 PersistenceContext 注释来做同样的事情:为每个线程注入一个应用程序管理的(= 不是容器管理的)EntityManager bean,就像 JavaEE 容器所做的那样。