JavaSE with CDI and JPA - Error WELD-001303

4

I am studying JPA and CDI in a Java SE application. When I create the EntityManagerFactory I come across the error:

Exception in thread "main" org.jboss.weld.context.ContextNotActiveException: WELD-001303: No active contexts for scope type javax.enterprise.context.RequestScoped

EntityMangerFactory:

@ApplicationScoped
public class EntityManagerProducer implements Serializable {

    private static final long serialVersionUID = 1L;

    @PersistenceUnit(unitName = "banco")
    private EntityManagerFactory factory;

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

    public void closeEntityManager(@Disposes EntityManager manager) {
        if (manager.isOpen()) {
            manager.close();
        }
    }
}

Main class:

public class Main {

    @Inject
    UserDao userDao;

    public void main(@Observes ContainerInitialized event, @Parameters List<String> params) {

        User u = new User();

        u.setName("usuario");
        u.setCpf("12345678911");
        u.setEmail("[email protected]");
        u.setLastName("rocha");
        u.setPassword("12345679");

        userDao.salvar(u);
    }
}

I changed the EntityManagerProducer , that way it works, but without the scopes. Is it correct?

public class EntityManagerProducer implements Serializable {

    private static final long serialVersionUID = 1L;

    @Produces
    public EntityManager createEntityManager() {
        return Persistence.createEntityManagerFactory("white-dragon").createEntityManager();
    }

    public void closeEntityManager(@Disposes EntityManager manager) {
        if (manager.isOpen()) {
            manager.close();
        }
    }
}
    
asked by anonymous 13.06.2015 / 22:58

1 answer

4

We found in the documentation that only @Application, @Dependent e @Singleton scopes are supported in SE environment.

@ApplicationScoped
public class EntityManagerProducer implements Serializable {

    private static final long serialVersionUID = 1L;

    @Produces
    public EntityManager createEntityManager() {
        return Persistence.createEntityManagerFactory("banco").createEntityManager();
    }

    public void closeEntityManager(@Disposes EntityManager manager) {
        if (manager.isOpen()) {
            manager.close();
        }
    }
}
    
14.06.2015 / 17:36