@Inject和@PersistenceContext有什么区别? [英] What is the difference between @Inject and @PersistenceContext?

查看:115
本文介绍了@Inject和@PersistenceContext有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用JPA的项目中,我通常使用

In a project using JPA, I commonly use

@Inject EntityManager em;

以获得这样的对象.我看到网络上的许多代码片段都改为使用:

in order to obtain such an object. I saw that many code snippets in the web instead use:

@PersistenceContext EntityManager em;

这些选项之间有什么区别?

What is the difference between these options?

我的代码在JBoss EAP 6.1和Hibernate上运行.

My code runs on JBoss EAP 6.1 and Hibernate.

推荐答案

@PersistenceContext是一个特定的批注,它声明对容器管理的实体管理器的依赖性.它允许您指定更多参数,例如持久性类型.当您要在有状态会话Bean的整个生命周期中维护持久性上下文时,将持久性类型设置为EXTENDED非常重要. @PersistenceContext是JPA批注.

@PersistenceContext is a specific annotation that declares a dependency on a container-managed entity manager. It allows you to specify more parameters like the persistence type. Setting the persistence type to EXTENDED is important when you want to maintain the persistence context for the whole life cycle of a stateful session bean. @PersistenceContext is a JPA annotation.

@Inject是CDI注释.它非常通用,可用于注入各种对象.

@Inject is a CDI annotation. It is very generic and can be used to inject a wide variety of objects.

这篇关于@Inject和@PersistenceContext有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆