Eclipse 中的 JPA 项目和 EJB 项目有什么区别?
什么时候用哪一个?
当我想要 JSF + Stateless Bean + JPA 实体应用程序时,我需要将 EJB 项目和动态 Web 项目集成到 EAR 项目中吗?
Which one is used when?
When I want JSF + Stateless Bean + JPA Entity application I need EJB Project and Dynamic Web Project integrates in EAR Project?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
EJB 项目是专注于 Enterprise Java Bean 开发的项目。通常,EJB 依赖于实体 Bean 来实现持久性,而实体 Bean 是使用 JPA 技术实现的(将 JPA 视为 EJB 的依赖项)。
您可以选择独立于 EJB 创建 JPA 项目的原因是,有些人可能不想使用(或不需要)EJB,但他们仍然需要使用 ORM 框架,例如 JPA。例如,这种情况可以是直接使用 JPA 进行持久性(无 EJB)的 Web 项目(即 JSF)。
重点是,尽管 EJB 3 使用 JPA 来实现持久性,但您不必在项目中使用 EJB(除非您需要这样做)才能从 JPA 等 ORM 框架中受益。
现在,要在 Eclipse 中的 EJB 项目上启用 JPA,请右键单击该项目,转到“Project Facets”选项并选择 JPA Facet。通过这样做,Eclipse 将添加所需的库、创建必要的工件 (persistence.xml) 并在 IDE 上启用 JPA 相关工具。
An EJB project is a project focused on the development of Enterprise Java Beans. Commonly, EJBs depend on Entitity Beans for persistence which are implemented using the JPA technology (think of JPA as a dependency of the EJB).
The reason that you have the option to create a JPA project independently of the EJB, is that some people might not want to use (or don't need to) EJBs but still, they need to use an ORM framework such as JPA. Such case could be for instance a web project (i.e. JSF) that would use JPA directly for persistence (no EJBs).
The takeaway point is that although EJB 3 used JPA for persistence, you don't have to necessarily use EJBs in your project (unless you need to do so) in order to benefit from an ORM framework such as JPA.
Now, to enable JPA on an EJB project in eclipse right-click on the project, go to "Project Facets" option and select the JPA facet. By doing so, eclipse will add the required libraries, create the necessary artifacts (persistence.xml) and enable the JPA related tooling on your IDE.
Java Persistence API(有时称为 JPA)是一种 Java 编程语言规范,它描述了使用 Java Platform, Standard Edition 和 Java Platform, Enterprise Edition 的应用程序中关系数据的管理。
Java Persistence API 最初是 JSR 220 专家组工作的一部分。 JPA 2.0 是 JSR 317 专家组的成果。
此上下文中的持久性涵盖三个领域:
Enterprise JavaBeans (EJB) 是由 Sun Microsystems 开发的 Java API,它定义了多层客户端/服务器系统的组件体系结构。
EJB 系统允许开发人员专注于模型的实际业务架构,而不用担心连接所有工作部分所需的无休止的编程和编码。这个任务留给了 EJB 服务器供应商。开发人员只需设计(或购买)所需的EJB组件并将它们安排在服务器上。
由于 EJB 系统是用 Java 编写的,因此它们是平台无关的。由于是面向对象的,它们可以实现到现有系统中,只需很少或无需重新编译和配置。
The Java Persistence API, sometimes referred to as JPA, is a Java programming language specification which describes the management of relational data in applications using Java Platform, Standard Edition and Java Platform, Enterprise Edition.
The Java Persistence API originated as part of the work of the JSR 220 Expert Group. JPA 2.0 is the work of the JSR 317 Expert Group.
Persistence in this context covers three areas:
Enterprise JavaBeans (EJB) is a Java API developed by Sun Microsystems that defines a component architecture for multi-tier client/server systems.
EJB systems allow developers to focus on the actual business architecture of the model, rather than worry about endless amounts of programming and coding needed to connect all the working parts. This task is left to EJB server vendors. Developers just design (or purchase) the needed EJB components and arrange them on the server.
Because EJB systems are written in Java, they are platform independent. Being object oriented, they can be implemented into existed systems with little or no recompiling and configuring.
这取决于您使用的 Java EE 版本。在 Java EE 6 中,我们可以将 EJB 放在 WAR 文件本身中。因此,一个动态 Web 项目就足够了。
如果您使用 Java EE 1.5,那么您需要有一个用于 JSF 的动态 Web 项目,对于无状态 bean,您需要有一个 EJB 项目,并且还需要一个企业项目来将 WAR 和 JAR 捆绑到 EAR 中。您可以在 EJB 项目本身中拥有与持久性相关的类,并且此处不需要 JPA 项目。
Java EE 提供了独立于 EJB 使用 JPA 的工具,因此如果您的应用程序不需要 EJB 但仍想使用 JPA,那么您可以选择 JPA 项目。
It depends on the version of Java EE you are using. In Java EE 6 we can have the EJBs in the WAR file itself. So one Dynamic web project would suffice.
If you are using Java EE 1.5 then you need to have a Dynamic Web project for JSF and for Stateless bean you need to have an EJB Project and also an enterprise project is need to bundle the WAR and JAR into an EAR. You can have persistence related classes in EJB project itself and dont require a JPA project here.
Java EE provides the facility to make use of JPA independent of EJBs so if your application does not require EJBs but still want to make use of JPA then you can go for a JPA project.