Javase with CDI and JPA - Error WELD-001303

Asked

Viewed 1,465 times

4

I am studying JPA and CDI in a Java SE application. When I will 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 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();
        }
    }
}
  • How are you starting the container in the main (the main(String[] args))? There is the beans.xml in its application?

  • @Brunocésar, I edited the question and entered the main. Yes, there is Beans.xml com bean-discovery-mode="all".

  • This you had already included. As there is no main (main(String[] args)), is probably starting its application with the StartMain, that’s it?

  • That’s right, start with Startmain. I’m sorry, I didn’t quite understand the question.

  • Right. To inject one EMF you need to be producing it, just as you produce the EM. How are you doing this? If not, this is the reason for the error. So, create a Produce for EMF also, or changes your createEntityManager for something like this: Persistence.createEntityManagerFactory("banco").createEntityManager();

  • I did that way public EntityManager createEntityManager() {&#xA; return Persistence.createEntityManagerFactory("banco").createEntityManager();&#xA; } The same error continues, but I took the @Requestscoped annotation and it worked normally. Because it is an SE application, it is not necessary to have Request, Application scopes etc.. ?

  • I saw your update. This way you can use @RequestScoped in createEntityManager, which is the usual. Tested this?

  • I tested yes, and also does not work, the exception is only not released without the two Annotations. But I did a test with @Dependent (it’s the standard scope, isn’t it?) and it worked normally.

  • Yeah, you don’t even need @Dependent, exactly for being standard. Looking at documentation, in environment JavaSE to @RequestedScoped does not work even in certain cases.

  • Really. I just looked at the documentation, and only the scopes work @Application, @Dependent, @Singleton What a distraction from me.

  • Wellington, mark your answer as the correct one, it can help other people :)

  • I’ll mark it, but for what it says in the message, I have to wait until tomorrow ahhahaha. Thanks for the help !!

Show 7 more comments

1 answer

4


We find in the documentation that only the scopes @Application, @Dependent e @Singleton 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();
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.