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 thebeans.xmlin its application?– Bruno César
@Brunocésar, I edited the question and entered the main. Yes, there is Beans.xml com
bean-discovery-mode="all".– Wellington Soares Nunes
This you had already included. As there is no main (
main(String[] args)), is probably starting its application with theStartMain, that’s it?– Bruno César
That’s right, start with Startmain. I’m sorry, I didn’t quite understand the question.
– Wellington Soares Nunes
Right. To inject one
EMFyou need to be producing it, just as you produce theEM. How are you doing this? If not, this is the reason for the error. So, create a Produce forEMFalso, or changes yourcreateEntityManagerfor something like this:Persistence.createEntityManagerFactory("banco").createEntityManager();– Bruno César
I did that way
public EntityManager createEntityManager() {
 return Persistence.createEntityManagerFactory("banco").createEntityManager();
 }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.. ?– Wellington Soares Nunes
I saw your update. This way you can use
@RequestScopedincreateEntityManager, which is the usual. Tested this?– Bruno César
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.
– Wellington Soares Nunes
Yeah, you don’t even need
@Dependent, exactly for being standard. Looking at documentation, in environmentJavaSEto@RequestedScopeddoes not work even in certain cases.– Bruno César
Really. I just looked at the documentation, and only the scopes work
@Application, @Dependent, @SingletonWhat a distraction from me.– Wellington Soares Nunes
Wellington, mark your answer as the correct one, it can help other people :)
– Bruno César
I’ll mark it, but for what it says in the message, I have to wait until tomorrow ahhahaha. Thanks for the help !!
– Wellington Soares Nunes