0

I have a trouble declaring a field as @EJB.

There is my LocalBean with the @LocalBean annotation

@LocalBean
@Stateless
public class PersonaBean {

    @PersistenceContext
    EntityManager em;
    
    public PersonaBean() {
        // TODO Auto-generated constructor stub
    }

There is another bean which uses the PersonaBean, using the @EJB. In this case, the field PersonaDAO isn´t giving null, that´s ok.

@Stateless
@LocalBean
public class GestionPersonaService implements Serializable{
    private static final long serialVersionUID = 1L;
    
    @EJB
    PersonaBean PersonaDAO;

    @EJB
    RolBean rolDAO;
    
    PersonaValidation personaValidation;
    /**
     * Default constructor. 
     */
    
    public GestionPersonaService() {
        personaValidation = new PersonaValidation();
    }

But in this class, where I also use the PersonaBean, in the field PersonaDAO, with the @EJB, that´s giving me a nullPointerException in PersonaDAO.

public class PersonaValidation {
    
    @EJB
    PersonaBean PersonaDAO;

    @EJB
    RolBean rolDAO;
    
    public PersonaValidation() {
        super();
    }
    
    public int validate(PersonaDTO personaDTO) throws PersonaException{
        try {
            PersonaTambo persona = PersonaDAO.personaPorNombreUsuario(personaDTO.getNombreUsuario());
            if (persona != null)
                if (persona.getEstado() == 1) {
                    throw new PersonaException("P001", PersonaException.USUARIO_EXISTE);
                }
                else
                    return HttpResponseCodes.SC_OK;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

0 Answers0