ejb容器中的Pojo类中的EntityManager为null [英] EntityManager is null inside Pojo class in ejb container

查看:180
本文介绍了ejb容器中的Pojo类中的EntityManager为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

但是em是空的。
我从bean调用TransactionManager类,如果我在bean类中声明EntityManager声明,那么它在bean类中就会很好地被注入。但是在POJO中它总是为空。



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



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



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

 code> @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类是我想要的类Dependency注入完成PersistenceContext

  public class TransactionManager扩展MyBase实现ITransactionManager 
{
@PersistenceContext(unitName = TestPU)
EntityManager em;
public void doSomething(X x)
{
em.persist(x);
}
}


解决方案

原因是您的POJO不受容器管理。如果您的容器支持CDI(上下文相关性注入),则在bean归档中声明您的bean(在META-INF或WEB-INF中具有beans.xml)以进行管理。


I have a Java web service module and ejb modulein netbeans (All part of an enterprise application). the web service is consuming the ejb class using @EJB injection. Inside the ejb module i have a TransactionManager class which is not an enterprise bean. just a POJO class. I am trying to inject the EntityManager using

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

but the em is allways null. I am calling the TransactionManager class from by bean, and if i declare the EntityManager declaration in bean class, it injects just fine in the bean class. but in POJO it is always null.

I am new to EJB (using version JEE7) . can u please guide me ?

Reply to Shailendra : my bean and pojo are in the same jar file, and it has the bean.xml and persistence.xml. I tried to make the TransactionManager class as bean, using @Stateless and @Local, but there are some pojo classes in between the ejb class and the TransactionManager class, and when i tried to access the TransactionManager class from its parent pojo class using @EJB TransactionManager transactionManager; this object too returned null.

Dear Shilendra, thanks for the reply Below is my EJB class

    @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);
        }
    }

I have a POJO class as MyComponent

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

And The TransactionManager class is the class that i want Dependency Injection done for PersistenceContext

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

解决方案

The reason is be that your POJO is not managed by the container. If your container supports CDI (Context dependency injection) then declare your bean in a bean archive (have beans.xml in META-INF or WEB-INF) in order to be managed.

这篇关于ejb容器中的Pojo类中的EntityManager为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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