Doubts with Crud java CDI

Asked

Viewed 191 times

1

I have connection with some database, with that, I made a class called connection, where I have verification of which database to use:

package DAO;

import CDI.Corporativo;
import CDI.Java3DS;
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;

    /**
     *
     * @author Windows
     */

    @Named
    @Dependent
    public class Conexao {

        @Inject
        @Corporativo //para injetar o EntityManager Corporativo(que acessa o BD do corporativo)
        private EntityManager em;

        @Inject
        @Java3DS //para injetar o EntityManager Corporativo(que acessa o BD do corporativo)
        private EntityManager em2;

        public EntityManager getEntityManager(String codgru){
            EntityManager entityManager = null;
            switch(codgru){
                case "FELI-9001":
                    entityManager = em;
                    break;
                case "FELI-9002":
                    entityManager = em2;
                    break;
            }
            return entityManager;

        }


    }

In my user DAO, I try to catch the entityManger concerning the connection by "codgru"

package DAO;

import Entity.Cadusr;


import javax.inject.Inject;


import javax.persistence.EntityManager;

import javax.persistence.Query;

public class UsuarioDao {

    @Inject
    private Conexao dao;

    public Cadusr buscaUsuario(String grupo, String usuario, String senha) {
        EntityManager em = dao.getEntityManager(grupo);
        Cadusr cadusr = null;

        if (em != null) {
            String jpql = "select a from Cadusr a where a.nomusr = :nomusr and a.pasusr = :pasusr and a.usratv = 'S'";
            Query query = em.createQuery(jpql, Cadusr.class);
            query.setParameter("nomusr", usuario);
            query.setParameter("pasusr", senha);
            try {
                cadusr = (Cadusr) query.getSingleResult();

            } catch (Exception e) {
            }
            if (cadusr == null) {
                return null;
            } else {
                return cadusr;
            }
        } else {
            return null;
        }
    }

}

and in my bean I do the checks:

package Controller;

import DAO.UsuarioDao;
import Entity.Cadusr;
import java.io.Serializable;
import java.util.List;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import javax.inject.Named;

@Named
@RequestScoped
public class mbean_001_Login implements Serializable {

    private String Usuario;
    private String Grupo;
    private String senha;

    UsuarioDao  usuarioDao = new UsuarioDao();


    public String Vusuario() {
        if (Grupo.equals("") || Usuario.equals("") || senha.equals("")) {
            if (Grupo.equals("")) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Grupo não Preenchido"));

            } else if (Usuario.equals("")) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Usúario não Preenchido"));

            } else if (senha.equals("")) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Senha não Preenchida"));

            }

            return "";

        } else {
            Cadusr cadusr = usuarioDao.buscaUsuario(Grupo, Usuario, senha);
            return "";

        }
    }

    public String getUsuario() {
        return Usuario;
    }

    public void setUsuario(String Usuario) {
        this.Usuario = Usuario;
    }

    public String getGrupo() {
        return Grupo;
    }

    public void setGrupo(String Grupo) {
        this.Grupo = Grupo;
    }

    public String getSenha() {
        return senha;
    }

    public void setSenha(String senha) {
        this.senha = senha;
    }

}

the problem is this, my entityManeger is coming as null when I create the dao to get from the connection the entityManger , if I put straight into the bean it will

follows the error

09:29:27,215 WARNING [javax.enterprise.Resource.webcontainer.jsf.Lifecycle] (default task-64) #{mbean_001_Login.Vusuario}: java.lang.Nullpointerexception: javax.faces.Facesexception: #{mbean_001_Login.Vusuario}: java.lang.Nullpointerexception at com.sun.faces.application.Actionlistenerimpl.processAction(Actionlistenerimpl.java:118) at javax.faces.Component.UICommand.broadcast(Uicommand.java:315) at javax.faces.Component.UIViewRoot.broadcastEvents(Uiviewroot.java:790) at javax.faces.Component.UIViewRoot.processApplication(Uiviewroot.java:1282) at com.sun.faces.lifecycle.Invokeapplicationphase.execute(Invokeapplicationphase.java:81) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) at com.sun.faces.lifecycle.Lifecycleimpl.execute(Lifecycleimpl.java:198) at javax.faces.webapp.FacesServlet.service(Facesservlet.java:658) at io.undertow.Servlet.handlers.Servlethandler.handleRequest(Servlethandler.java:85) at io.undertow.Servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(Servletsecurityrolehandler.java:62) at io.undertow.Servlet.handlers.Servletdispatchinghandler.handleRequest(Servletdispatchinghandler.java:36) at org.wildfly.Extension.undertow.security.SecurityContextAssociationHandler.handleRequest(Securitycontextassociationhandler.java:78) at io.undertow.server.handlers.Predicatehandler.handleRequest(Predicatehandler.java:43) at io.undertow.Servlet.handlers.security.SLInformationAssociationHandler.handleRequest(Sslinformationassociationhandler.java:131) at io.undertow.Servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(Servletauthenticationcallhandler.java:57) at io.undertow.server.handlers.Predicatehandler.handleRequest(Predicatehandler.java:43) at io.undertow.security.handlers.Abstractconfidentialityhandler.handleRequest(Abstractconfidentialityhandler.java:46) at io.undertow.Servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(Servletconfidentialityconstrainthandler.java:64) at io.undertow.security.handlers.Authenticationmechanismshandler.handleRequest(Authenticationmechanismshandler.java:60) at io.undertow.Servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(Cachedauthenticatedsessionhandler.java:77) at io.undertow.security.handlers.Notificationreceiverhandler.handleRequest(Notificationreceiverhandler.java:50) at io.undertow.security.handlers.Abstractsecuritycontextassociationhandler.handleRequest(Abstractsecuritycontextassociationhandler.java:43) at io.undertow.server.handlers.Predicatehandler.handleRequest(Predicatehandler.java:43) at org.wildfly.Extension.undertow.security.jacc.Jacccontextidhandler.handleRequest(Jacccontextidhandler.java:61) at io.undertow.server.handlers.Predicatehandler.handleRequest(Predicatehandler.java:43) at io.undertow.server.handlers.Predicatehandler.handleRequest(Predicatehandler.java:43) at io.undertow.Servlet.handlers.Servletinitialhandler.handleFirstRequest(Servletinitialhandler.java:292) at io.undertow.Servlet.handlers.Servletinitialhandler.access$100(Servletinitialhandler.java:81) at io.undertow.Servlet.handlers.Servletinitialhandler$2.call(Servletinitialhandler.java:138) at io.undertow.Servlet.handlers.Servletinitialhandler$2.call(Servletinitialhandler.java:135) at io.undertow.Servlet.core.Servletrequestcontextthreadsetupaction$1.call(Servletrequestcontextthreadsetupaction.java:48) at io.undertow.Servlet.core.Contextclassloadersetupaction$1.call(Contextclassloadersetupaction.java:43) at io.undertow.Servlet.api.Legacythreadsetupactionwrapper$1.call(Legacythreadsetupactionwrapper.java:44) at io.undertow.Servlet.api.Legacythreadsetupactionwrapper$1.call(Legacythreadsetupactionwrapper.java:44) at io.undertow.Servlet.api.Legacythreadsetupactionwrapper$1.call(Legacythreadsetupactionwrapper.java:44) at io.undertow.Servlet.api.Legacythreadsetupactionwrapper$1.call(Legacythreadsetupactionwrapper.java:44) at io.undertow.Servlet.api.Legacythreadsetupactionwrapper$1.call(Legacythreadsetupactionwrapper.java:44) at io.undertow.Servlet.api.Legacythreadsetupactionwrapper$1.call(Legacythreadsetupactionwrapper.java:44) at io.undertow.Servlet.handlers.Servletinitialhandler.dispatchRequest(Servletinitialhandler.java:272) at io.undertow.Servlet.handlers.Servletinitialhandler.access$000(Servletinitialhandler.java:81) at io.undertow.Servlet.handlers.Servletinitialhandler$1.handleRequest(Servletinitialhandler.java:104) at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202) at io.undertow.server.Httpserverexchange$1.run(Httpserverexchange.java:805) at java.util.Concurrent.ThreadPoolExecutor.runWorker(Threadpoolexecutor.java:1142) at java.util.Concurrent.Threadpoolexecutor$Worker.run(Threadpoolexecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: javax.faces.el.Evaluationexception: java.lang.Nullpointerexception at javax.faces.Component.MethodBindingMethodExpressionAdapter.invoke(Methodbindingmethodexpressed dapter.java:101) at com.sun.faces.application.Actionlistenerimpl.processAction(Actionlistenerimpl.java:102) ... 45 more Caused by: java.lang.Nullpointerexception at DAO.UsuarioDao.buscaUsuario(Usuariodao.java:23) at Controller.mbean_001_Login.Vusuario(mbean_001_Login.java:45) at sun.reflect..Nativemethodaccessorimpl.invoke0(Native Method) at sun.reflect.Nativemethodaccessorimpl.invoke(Nativemethodaccessorimpl.java:62) at sun.reflect.Delegatingmethodaccessorimpl.invoke(Delegatingmethodaccessorimpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.sun.el.util.Reflectionutil.invokeMethod(Reflectionutil.java:181) at com.sun.el.parser.Astvalue.invoke(Astvalue.java:289) at com.sun.el.MethodExpressionImpl.invoke(Methodexpressionimpl.java:304)

09:29:27,388 SEVERE [javax.enterprise.Resource.webcontainer.jsf.context] (default task-64) javax.faces.el.Evaluationexception: java.lang.Nullpointerexception at javax.faces.Component.MethodBindingMethodExpressionAdapter.invoke(Methodbindingmethodexpressed dapter.java:101) at com.sun.faces.application.Actionlistenerimpl.processAction(Actionlistenerimpl.java:102) at javax.faces.Component.UICommand.broadcast(Uicommand.java:315) at javax.faces.Component.UIViewRoot.broadcastEvents(Uiviewroot.java:790) at javax.faces.Component.UIViewRoot.processApplication(Uiviewroot.java:1282) at com.sun.faces.lifecycle.Invokeapplicationphase.execute(Invokeapplicationphase.java:81) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) at com.sun.faces.lifecycle.Lifecycleimpl.execute(Lifecycleimpl.java:198) at javax.faces.webapp.FacesServlet.service(Facesservlet.java:658) at io.undertow.Servlet.handlers.Servlethandler.handleRequest(Servlethandler.java:85) at io.undertow.Servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(Servletsecurityrolehandler.java:62) at io.undertow.Servlet.handlers.Servletdispatchinghandler.handleRequest(Servletdispatchinghandler.java:36) at org.wildfly.Extension.undertow.security.SecurityContextAssociationHandler.handleRequest(Securitycontextassociationhandler.java:78) at io.undertow.server.handlers.Predicatehandler.handleRequest(Predicatehandler.java:43) at io.undertow.Servlet.handlers.security.SLInformationAssociationHandler.handleRequest(Sslinformationassociationhandler.java:131) at io.undertow.Servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(Servletauthenticationcallhandler.java:57) at io.undertow.server.handlers.Predicatehandler.handleRequest(Predicatehandler.java:43) at io.undertow.security.handlers.Abstractconfidentialityhandler.handleRequest(Abstractconfidentialityhandler.java:46) at io.undertow.Servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(Servletconfidentialityconstrainthandler.java:64) at io.undertow.security.handlers.Authenticationmechanismshandler.handleRequest(Authenticationmechanismshandler.java:60) at io.undertow.Servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(Cachedauthenticatedsessionhandler.java:77) at io.undertow.security.handlers.Notificationreceiverhandler.handleRequest(Notificationreceiverhandler.java:50) at io.undertow.security.handlers.Abstractsecuritycontextassociationhandler.handleRequest(Abstractsecuritycontextassociationhandler.java:43) at io.undertow.server.handlers.Predicatehandler.handleRequest(Predicatehandler.java:43) at org.wildfly.Extension.undertow.security.jacc.Jacccontextidhandler.handleRequest(Jacccontextidhandler.java:61) at io.undertow.server.handlers.Predicatehandler.handleRequest(Predicatehandler.java:43) at io.undertow.server.handlers.Predicatehandler.handleRequest(Predicatehandler.java:43) at io.undertow.Servlet.handlers.Servletinitialhandler.handleFirstRequest(Servletinitialhandler.java:292) at io.undertow.Servlet.handlers.Servletinitialhandler.access$100(Servletinitialhandler.java:81) at io.undertow.Servlet.handlers.Servletinitialhandler$2.call(Servletinitialhandler.java:138) at io.undertow.Servlet.handlers.Servletinitialhandler$2.call(Servletinitialhandler.java:135) at io.undertow.Servlet.core.Servletrequestcontextthreadsetupaction$1.call(Servletrequestcontextthreadsetupaction.java:48) at io.undertow.Servlet.core.Contextclassloadersetupaction$1.call(Contextclassloadersetupaction.java:43) at io.undertow.Servlet.api.Legacythreadsetupactionwrapper$1.call(Legacythreadsetupactionwrapper.java:44) at io.undertow.Servlet.api.Legacythreadsetupactionwrapper$1.call(Legacythreadsetupactionwrapper.java:44) at io.undertow.Servlet.api.Legacythreadsetupactionwrapper$1.call(Legacythreadsetupactionwrapper.java:44) at io.undertow.Servlet.api.Legacythreadsetupactionwrapper$1.call(Legacythreadsetupactionwrapper.java:44) at io.undertow.Servlet.api.Legacythreadsetupactionwrapper$1.call(Legacythreadsetupactionwrapper.java:44) at io.undertow.Servlet.api.Legacythreadsetupactionwrapper$1.call(Legacythreadsetupactionwrapper.java:44) at io.undertow.Servlet.handlers.Servletinitialhandler.dispatchRequest(Servletinitialhandler.java:272) at io.undertow.Servlet.handlers.Servletinitialhandler.access$000(Servletinitialhandler.java:81) at io.undertow.Servlet.handlers.Servletinitialhandler$1.handleRequest(Servletinitialhandler.java:104) at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202) at io.undertow.server.Httpserverexchange$1.run(Httpserverexchange.java:805) at java.util.Concurrent.ThreadPoolExecutor.runWorker(Threadpoolexecutor.java:1142) at java.util.Concurrent.Threadpoolexecutor$Worker.run(Threadpoolexecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.Nullpointerexception at DAO.UsuarioDao.buscaUsuario(Usuariodao.java:23) at Controller.mbean_001_Login.Vusuario(mbean_001_Login.java:45) at sun.reflect..Nativemethodaccessorimpl.invoke0(Native Method) at sun.reflect.Nativemethodaccessorimpl.invoke(Nativemethodaccessorimpl.java:62) at sun.reflect.Delegatingmethodaccessorimpl.invoke(Delegatingmethodaccessorimpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.sun.el.util.Reflectionutil.invokeMethod(Reflectionutil.java:181) at com.sun.el.parser.Astvalue.invoke(Astvalue.java:289) at com.sun.el.MethodExpressionImpl.invoke(Methodexpressionimpl.java:304) at org.jboss.Weld.util.el.ForwardingMethodExpression.invoke(Forwardingmethodexpression.java:40) at org.jboss.Weld.el.Weldmethodexpression.invoke(Weldmethodexpression.java:50) at org.jboss.Weld.util.el.ForwardingMethodExpression.invoke(Forwardingmethodexpression.java:40) at org.jboss.Weld.el.Weldmethodexpression.invoke(Weldmethodexpression.java:50) at com.sun.faces.facelets.el.TagMethodExpression.invoke(Tagmethodexpression.java:105) at javax.faces.Component.MethodBindingMethodExpressionAdapter.invoke(Methodbindingmethodexpressed dapter.java:87) ... 46 more

  • What is coming null?

  • my entityManager

  • Edit the question by arranging the final sentence with this information, It was kind of meaningless as it is.

  • if (cadusr == null) { Return null; } Else { Return cadusr; } This can be summarized as Return cadusr;

  • Gives a System.out.println(e.getMessage()) to see the exception message when falling there in the exception. See the error message will facilitate the debug.

  • when I leave this way it doesn’t even enter my class connected , simply gives the error in the method , I will post together the error

  • Problem solved, I just had to inject my dao into the bean... the brigand

Show 2 more comments

1 answer

0

Your User Bean is not being injected by CDI.

UsuarioDao  usuarioDao = new UsuarioDao();

Use @Inject to inject it, so CDI injects all its dependencies.

Browser other questions tagged

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