【问题标题】:EntityManager is null inside Pojo class in ejb containerEntityManager 在 ejb 容器的 Pojo 类中为空
【发布时间】:2013-10-16 07:46:04
【问题描述】:

我在 netbeans 中有一个 Java Web 服务模块和 ejb 模块(企业应用程序的所有部分)。 Web 服务正在使用 @EJB 注入来使用 ejb 类。 在 ejb 模块中,我有一个不是企业 bean 的 TransactionManager 类。只是一个 POJO 类。我正在尝试使用

注入 EntityManager
    @PersistanceContext(unitName = "testPU")
    EntityManager em;

但 em 始终为空。 我从 bean 调用 TransactionManager 类,如果我在 bean 类中声明 EntityManager 声明,它在 bean 类中注入就好了。但在 POJO 中它始终为空。

我是 EJB 新手(使用 JEE7 版本)。你能指导我吗?

回复 Shailendra : 我的 bean 和 pojo 在同一个 jar 文件中,它有 bean.xml 和 persistence.xml。 我尝试使用@Stateless 和@Local 将TransactionManager 类作为bean,但是在ejb 类和TransactionManager 类之间有一些pojo 类,当我尝试使用它从其父pojo 类访问TransactionManager 类时 @EJB 事务管理器事务管理器; 这个对象也返回了 null。

亲爱的 Shilendra,感谢您的回复 下面是我的 EJB 类

    @Stateless
    @Local(IMyService.class)
    public class MyService extends MyBase implements IMyService
    {
        MyComponent component = null;
        public void doSomething(X x)
        {
             component = new MyComponent();
             component.doSomething(x);
        }
    }

我有一个 POJO 类作为 MyComponent

    public class MyComponent extends MyBase implements IMyComponent
    {
        TransactionManager tManager = null;
        public void doSomething(X x)
        {
            tManager = new TransactionManager();
            tManager.doSomething(x);
        }
    }

TransactionManager 类是我希望为 PersistenceContext 完成依赖注入的类

    public class TransactionManager extends MyBase implements ITransactionManager
    {
          @PersistenceContext(unitName="TestPU")
          EntityManager em;
          public void doSomething(X x)
          {
               em.persist(x);
          }
     }

【问题讨论】:

    标签: jakarta-ee jpa ejb entitymanager


    【解决方案1】:

    POJO 不受容器管理。而 bean 由容器管理并负责注入资源。

    您可以尝试 JNDI 查找来查找容器不负责的非托管自定义类的资源。

    【讨论】:

    • 你好,可以给我一份样品
    • @vilsad 我对 CDI 做的不多,你可以试试 @Inject EntityManager 看看是否有效。
    【解决方案2】:

    原因是你的 POJO 不是由容器管理的。如果您的容器支持 CDI(上下文依赖注入),则在 bean 存档中声明您的 bean(在 META-INF 或 WEB-INF 中有 beans.xml)以便进行管理。

    【讨论】:

    • 能否贴出相关的类结构
    • 我发布了这个问题的 tge 相关代码,在搜索时我得到一个谣言,这个问题是 GlassFish 特有的(一些搜索指出了这一点)。但我无法用另一台服务器对其进行测试,因为我找不到与 JEE7 兼容的服务器(Tomcat / JBoss)。
    • 我试图通过将 (at) Stateless 添加到类声明的顶部和 (at) Local 到接口声明的顶部来将 TransactionManager 类作为 bean。然后通过注入(at)PersistenceUnit 来获取 EntityManagerFactory。但这也返回了 null;
    【解决方案3】:

    您应该使用@Inject 注解,而不是使用 new-operator 创建实例。

    1. 创建\WEB-INF\beans.xml(Netbeans 中有一个向导)并设置 bean-discovery-mode="all"

    2. 这是您的“MyService”的代码:您的还是我的? :-)

       @Stateless
       @Local(IMyService.class)
       public class MyService extends MyBase implements IMyService {
      
           @Inject
           private MyComponent component;
      
           public void doSomething(X x)
           {
                component = new MyComponent();
                component.doSomething(x);
           }
       }
      
    3. 这是你的“我的组件”:-)

      public class MyComponent extends MyBase implements IMyComponent
      {
          @Inject 
          private TransactionManager tManager;
      
          public void doSomething(X x)
          {
              tManager = new TransactionManager();
              tManager.doSomething(x);
          }
      }
      

    【讨论】:

    • 您能接受答案吗?只需点击答案旁边的灰色复选标记即可。
    猜你喜欢
    • 1970-01-01
    • 2011-05-05
    • 2019-06-15
    • 2019-04-03
    • 2021-05-02
    • 2015-07-05
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多