简体   繁体   English

CDI不注入entitymanager

[英]cdi does not inject entitymanager

cdi don't injects entitymanager, always the nullpointer. cdi不会注入entitymanager,总是nullpointer。 Following configuration: 配置如下:

persistence.xml persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
 <persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    version="1.0">
    <persistence-unit name="tutoroo" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:/tutoroo</jta-data-source>
        <properties>
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="false" />
            <property name="hibernate.hbm2ddl.auto" value="create-drop" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
        </properties>
    </persistence-unit>
 </persistence>

public class ProdutorEntityManager implements Serializable {

    private EntityManagerFactory factory = Persistence.createEntityManagerFactory("tutoroo");
   //private EntityManager entityManager = factory.createEntityManager();

    @Produces
    @PersistenceContext
    @RequestScoped
    public EntityManager criaEntityManager(){
        return factory.createEntityManager();
    }

    public void dispose(@Disposes EntityManager em) {
        em.close();
    }
}

public class UsuarioDaoImp implements UsuarioDao {

    @Inject
    private EntityManager manager; 

    public void salvar(Usuario usuario) {
        manager.persist(usuario);
    }
}

When I debug the EntityManager UsuarioDaoImp class, this exception occurs: com.sun.jdi.InvocationException occurred invoking method. 当我调试EntityManager UsuarioDaoImp类时,会发生此异常:com.sun.jdi.InvocationException发生了调用方法。

I do not know what I'm doing wrong. 我不知道我在做什么错。 Can anyone help? 有人可以帮忙吗?

Server is: jboss-as-7.1.1 服务器是:jboss-as-7.1.1

First off, don't create the persistence units yourself in an app server, but let the server inject it for you. 首先,不要自己在应用服务器中创建持久性单元,而是让服务器为您注入持久性单元。

Here's why, from JavaDocs : 这就是从JavaDocs来的原因:

The Persistence class is available in a Java EE container environment as well; Persistence类在Java EE容器环境中也可用。 however, support for the Java SE bootstrapping APIs is not required in container environments. 但是,在容器环境中不需要支持Java SE自举API。

Not sure how jbos-as-7 behaves, but it is generally discouraged because of reasons such as loosing JTA support. 不知道jbos-as-7的行为如何,但是由于诸如失去JTA支持之类的原因,一般不建议这样做。

For simplicity, I assume you only have one persistence unit in your application. 为简单起见,我假设您的应用程序中只有一个持久性单元。 Please ask and I'll edit if you need examples for an application with multiple persistence units. 请询问,如果需要具有多个持久性单元的应用程序示例,我将进行编辑。

To simply use the entity manager in any CDI managed bean: 要简单地在任何 CDI托管bean中使用实体管理器:

public class CDIBean {

  // the container injects it
  @PersistenceContext
  private EntityManager em;

  // just use it
  public void someMethod(Entity someEntity) {
    this.em.persist(someEntity);
  }

}

That's all there is to it. 这里的所有都是它的。

However, in many examples, a combination of producers / disposers are declared for various reasons. 但是,在许多示例中,出于各种原因声明了生产者/处置者的组合。 I bet this is where the confusion comes from. 我敢打赌这就是混乱的根源。 Some of the use cases: 一些用例:

  • To allow you to use @Inject EntityManger em; 允许您使用@Inject EntityManger em; instead of @PersistenceContext EntityManager em; 而不是@PersistenceContext EntityManager em;

     // to make it available for injection using @Inject public class CDIProducer { // again, the container injects it @PersistenceContext private EntityManager em; // this will have the default dependent scope @Produces public EntityManager em() { return em; } public void dispose(@Disposes EntityManager em) { em.close(); } } // to use it public class CDIBean { @Inject private EntityManager em; // just use it public void someMethod(Entity someEntity) { this.em.persist(someEntity); } } 
  • or to bind an entity manager to a particular scope. 或将实体管理器绑定到特定范围。

     // to make it available for injection using @Inject, and bind it to the @RequestScope public class CDIProducer { // again, the container injects it @PersistenceContext private EntityManager em; // this will be in the request scope @Produces @RequestScoped public EntityManager em() { return em; } public void dispose(@Disposes @RequestScoped EntityManager em) { em.close(); } } // to use it public class CDIBean { @Inject private EntityManager em; // just use it public void someMethod(Entity someEntity) { this.em.persist(someEntity); } } 

Finally, the method producers above can be converted to field producers. 最后,上述方法生产者可以转换为现场生产者。 This is equivalent to the last example: 这等效于最后一个示例:

// to make it available for injection using @Inject, and bind it to the @RequestScope
public class CDIProducer {

  @PersistenceContext
  @Produces
  @RequestScoped
  private EntityManager em;

  public void dispose(@Disposes @RequestScoped EntityManager em) {
    em.close();
  }

}

我认为@RequestScoped不允许作为参数注入。

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

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