Enterprise Java Bean(EJB)和Entity Java Bean有什么区别?

时间:2021-03-30 20:08:38

Can anyone explain me the difference between Enterprise Java Beans and Entity Java Bean?

谁能解释一下Enterprise Java Beans和Entity Java Bean之间的区别?

I know what EJB is but I don't understand their difference in regard to persistence.

我知道EJB是什么,但我不理解它们在持久性方面的区别。

We use the same annotations to save it, but then why call it different?

我们使用相同的注释来保存它,但为什么称它为不同?

1 个解决方案

#1


2  

In the old days, as the earth was forming, Java EE had two basic kinds of Beans -- the Session Bean, and the Entity Bean.

在过去,随着地球的形成,Java EE有两种基本类型的Bean - 会话Bean和实体Bean。

The Session Bean is an interface to some kind of internal service. Most every framework has some basic parallel to an Session Bean.

会话Bean是某种内部服务的接口。大多数每个框架都与会话Bean有一些基本的并行。

The Entity Bean was a persistent element managed by the container.

实体Bean是由容器管理的持久元素。

When people talk about Java EE, and especially when they gripe about it, Entity Beans were a core problem. They simply weren't very good.

当人们谈论Java EE时,特别是当他们抱怨它时,Entity Beans是一个核心问题。他们根本不是很好。

With the introduction of the Java Persistence Architecture (JPA), with frameworks like Hibernate and EclipseLink, the Java EE view of managed persistence pivoted dramatically. No longer did we have "heavyweight" constructs such as Entity Beans, but rather light weight POJOs managed by the JPA.

随着Java持久性架构(JPA)的引入,以及Hibernate和EclipseLink等框架,托管持久性的Java EE视图发生了巨大变化。我们不再拥有像Entity Beans这样的“重量级”构造,而是由JPA管理的轻量级POJO。

The confusion, however, is that objects that are managed by the JPA are referred to as Entities. A JPA Entity and an EJB Entity Bean are completely different animals. But the reuse of the term is a source of confusion.

然而,令人困惑的是,由JPA管理的对象被称为实体。 JPA实体和EJB实体Bean是完全不同的动物。但该术语的重用是混乱的根源。

With an EJB Entity Bean, and EJB and an Entity are the same thing. A Entity EJB IS an EJB, like a Session Bean.

使用EJB实体Bean,EJB和实体是一回事。实体EJB是一个EJB,就像会话Bean一样。

A JPA Entity, however, is not. Technically, an Entity is not related to EJB at all. The JPA Entity Manager is integrated within the EJB runtime context (and by projection, any Entity managed by that Entity Manager is part of the EJB runtime context), but there's no requirement for JPA to be used within an EJB container. They're separate technologies. That said, they do work quite well within an EJB container.

但是,JPA实体不是。从技术上讲,实体根本与EJB无关。 JPA实体管理器集成在EJB运行时上下文中(并且通过投影,由该实体管理器管理的任何实体都是EJB运行时上下文的一部分),但是不要求在EJB容器中使用JPA。它们是独立的技术。也就是说,它们在EJB容器中运行良好。

So.

Today, Entity EJBs still exist, but are deprecated, and will some day go away. But the containers still support them. There is no reason to pay them any mind whatsoever unless you have some blob of legacy code. Excluding the Entity Bean, Java EE supports: Stateless Session Beans, Stateful Session Beans, and Message Driven Beans. These are all first class EJBs which represent the heart of the Java EE component model. The most notable aspect of an EJB at runtime is how they engage with the local transaction space managed within the container. Also, EJBs are a deployable construct within the EJB container, similar to a WAR. (They are also other things, this is hardly exhaustive.)

今天,实体EJB仍然存在,但已被弃用,并且有一天会消失。但容器仍然支持它们。除非你有一些遗留代码,否则没有理由付钱给他们。除了实体Bean之外,Java EE还支持:无状态会话Bean,有状态会话Bean和消息驱动Bean。这些都是一流的EJB,它们代表了Java EE组件模型的核心。 EJB在运行时最值得注意的方面是它们如何与容器内管理的本地事务空间进行交互。此外,EJB是EJB容器中的可部署构造,类似于WAR。 (它们也是其他的东西,这几乎不详尽。)

JPA Entities are not EJBs. They have no transactional context. They have a different state, as to whether they are actively managed or not by their Entity Manager. The Entity Manager is enrolled in the local transaction space (and thus the JPA managed Entities are as well, by proxy).

JPA实体不是EJB。他们没有交易背景。他们有不同的状态,他们是否由他们的实体经理主动管理。实体管理器注册在本地事务空间中(因此JPA管理的实体也是通过代理)。

Finally, with CDI, and the rise of annotations, the embedding of EJBs directly within WARs, the boundaries that distinguish EJBs themselves are getting fuzzier and fuzzier every day.

最后,随着CDI和注释的兴起,EJB直接嵌入到WAR中,区分EJB本身的界限每天都变得越来越模糊和模糊。

#1


2  

In the old days, as the earth was forming, Java EE had two basic kinds of Beans -- the Session Bean, and the Entity Bean.

在过去,随着地球的形成,Java EE有两种基本类型的Bean - 会话Bean和实体Bean。

The Session Bean is an interface to some kind of internal service. Most every framework has some basic parallel to an Session Bean.

会话Bean是某种内部服务的接口。大多数每个框架都与会话Bean有一些基本的并行。

The Entity Bean was a persistent element managed by the container.

实体Bean是由容器管理的持久元素。

When people talk about Java EE, and especially when they gripe about it, Entity Beans were a core problem. They simply weren't very good.

当人们谈论Java EE时,特别是当他们抱怨它时,Entity Beans是一个核心问题。他们根本不是很好。

With the introduction of the Java Persistence Architecture (JPA), with frameworks like Hibernate and EclipseLink, the Java EE view of managed persistence pivoted dramatically. No longer did we have "heavyweight" constructs such as Entity Beans, but rather light weight POJOs managed by the JPA.

随着Java持久性架构(JPA)的引入,以及Hibernate和EclipseLink等框架,托管持久性的Java EE视图发生了巨大变化。我们不再拥有像Entity Beans这样的“重量级”构造,而是由JPA管理的轻量级POJO。

The confusion, however, is that objects that are managed by the JPA are referred to as Entities. A JPA Entity and an EJB Entity Bean are completely different animals. But the reuse of the term is a source of confusion.

然而,令人困惑的是,由JPA管理的对象被称为实体。 JPA实体和EJB实体Bean是完全不同的动物。但该术语的重用是混乱的根源。

With an EJB Entity Bean, and EJB and an Entity are the same thing. A Entity EJB IS an EJB, like a Session Bean.

使用EJB实体Bean,EJB和实体是一回事。实体EJB是一个EJB,就像会话Bean一样。

A JPA Entity, however, is not. Technically, an Entity is not related to EJB at all. The JPA Entity Manager is integrated within the EJB runtime context (and by projection, any Entity managed by that Entity Manager is part of the EJB runtime context), but there's no requirement for JPA to be used within an EJB container. They're separate technologies. That said, they do work quite well within an EJB container.

但是,JPA实体不是。从技术上讲,实体根本与EJB无关。 JPA实体管理器集成在EJB运行时上下文中(并且通过投影,由该实体管理器管理的任何实体都是EJB运行时上下文的一部分),但是不要求在EJB容器中使用JPA。它们是独立的技术。也就是说,它们在EJB容器中运行良好。

So.

Today, Entity EJBs still exist, but are deprecated, and will some day go away. But the containers still support them. There is no reason to pay them any mind whatsoever unless you have some blob of legacy code. Excluding the Entity Bean, Java EE supports: Stateless Session Beans, Stateful Session Beans, and Message Driven Beans. These are all first class EJBs which represent the heart of the Java EE component model. The most notable aspect of an EJB at runtime is how they engage with the local transaction space managed within the container. Also, EJBs are a deployable construct within the EJB container, similar to a WAR. (They are also other things, this is hardly exhaustive.)

今天,实体EJB仍然存在,但已被弃用,并且有一天会消失。但容器仍然支持它们。除非你有一些遗留代码,否则没有理由付钱给他们。除了实体Bean之外,Java EE还支持:无状态会话Bean,有状态会话Bean和消息驱动Bean。这些都是一流的EJB,它们代表了Java EE组件模型的核心。 EJB在运行时最值得注意的方面是它们如何与容器内管理的本地事务空间进行交互。此外,EJB是EJB容器中的可部署构造,类似于WAR。 (它们也是其他的东西,这几乎不详尽。)

JPA Entities are not EJBs. They have no transactional context. They have a different state, as to whether they are actively managed or not by their Entity Manager. The Entity Manager is enrolled in the local transaction space (and thus the JPA managed Entities are as well, by proxy).

JPA实体不是EJB。他们没有交易背景。他们有不同的状态,他们是否由他们的实体经理主动管理。实体管理器注册在本地事务空间中(因此JPA管理的实体也是通过代理)。

Finally, with CDI, and the rise of annotations, the embedding of EJBs directly within WARs, the boundaries that distinguish EJBs themselves are getting fuzzier and fuzzier every day.

最后,随着CDI和注释的兴起,EJB直接嵌入到WAR中,区分EJB本身的界限每天都变得越来越模糊和模糊。