简体   繁体   中英

cdi does not inject entitymanager

cdi don't injects entitymanager, always the nullpointer. Following configuration:

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.

I do not know what I'm doing wrong. Can anyone help?

Server is: 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 :

The Persistence class is available in a Java EE container environment as well; however, support for the Java SE bootstrapping APIs is not required in container environments.

Not sure how jbos-as-7 behaves, but it is generally discouraged because of reasons such as loosing JTA support.

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:

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; instead of @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不允许作为参数注入。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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