简体   繁体   English

在容器外使用 EJB

[英]Using an EJB outside container

I am currently creating a J2EE application and there is a part of it that is running outside the container, using a ServletContextListener to launch it.我目前正在创建一个 J2EE 应用程序,它的一部分在容器外运行,使用 ServletContextListener 来启动它。

However I also need to access the database from this part.但是我还需要从这部分访问数据库。

I currently have an Entity and a Stateless Session bean to fetch use the EntityManager.我目前有一个实体和一个无状态 Session bean 来获取使用 EntityManager。

I tested multiple things ( EntityManagerFactory, Initial Context, EJBContainer ) but I didn't manage to make any of them work.我测试了多个东西(EntityManagerFactory、Initial Context、EJBContainer),但我没有设法让它们中的任何一个工作。

How do I need to do it?我需要怎么做?

You do not need EJB, actually you cannot create Ejbs outside the container.您不需要 EJB,实际上您无法在容器外创建 Ejb。 You need JPA, an OR-mapper and JDBC.您需要 JPA、一个 OR 映射器和 JDBC。 These normally are correctly configured in your EJB-Container.这些通常在您的 EJB 容器中正确配置。 Outside the container you have to do that yourself.在容器之外,你必须自己做。

You have to define your dependencies right, so that the correct JDBC-Driver is available and the OR-Mapper (probably eclipselink or hibernate?) After that, you need define a presistence.xml to define the Entities to be used and to define how the DBMS is accessed via JDBC.您必须正确定义依赖项,以便正确的 JDBC-Driver 可用和 OR-Mapper(可能是 eclipselink 或 hibernate?)之后,您需要定义一个 presistence.xml 来定义要使用的实体并定义如何使用DBMS 通过 JDBC 访问。

If that all is correctly configured EntityManagerFactory is the correct way to create an EntityManger for the persistence-unit defined in persistence.xml.如果一切都正确配置,EntityManagerFactory 是为 persistence.xml 中定义的持久性单元创建 EntityManger 的正确方法。

There are many examples available on the net.网上有很多例子。 eg:例如:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM