could not execute statement

Asked

Viewed 1,178 times

0

I am using JAVAJPA, Wildfly and Primefaces, when I try to save in the database it gives me this error: "could not execute statement". Someone can help me?

This is my MB:

package br.com.sicoob.ManagedBeans ;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import br.com.sicoob.Service.UsuariosService;
import br.com.sicoob.entidades.Usuarios;


@ManagedBean(name="usuariosMB")
@ViewScoped
public class UsuariosMB implements Serializable {



    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @EJB
    private UsuariosService usuariosService;

    private Usuarios usuarios = new Usuarios();
    private List<Usuarios> listaUsuarios = new ArrayList<Usuarios>();
    private boolean edicao = false;

    @PostConstruct
    public void init() {
        listaUsuarios = usuariosService.listarTodos();
    }

    /**
     * Método responsavel por adicionar usuarios
     */

    public void adicionar() {

        if(validarUsuario(usuarios) == false) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Favor preencher todos os campos"));
            return;
        }

        usuariosService.salvarUsuario(usuarios);
        listaUsuarios = usuariosService.listarTodos();

        usuarios = new Usuarios();
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "", "Usuário adicionado com sucesso"));
    }

    /**
     * 
     * @param usuario
     * @return
     */
    private boolean validarUsuario(Usuarios usuario) {
        if(usuario.getNome() == null || "".equals(usuario.getNome())) {
            return false;
        }
        if(usuario.getEmail() == null || "".equals(usuario.getEmail())) {
            return false;
        }
        if(usuario.getTelefone() == null || "".equals(usuario.getTelefone())) {
            return false;
        }
        return true;
    }


    /**
     * 
     * @return
     */
    public String redirecionar() {
        return "perfil";

    }

    /**
     * Método responsavel por adicionar usuarios
     */
    public void atualizar() {
        usuariosService.salvarUsuario(usuarios);
        usuarios = new Usuarios();
        edicao = false;
    }

    /**
     * 
     * @param u
     */
    public void remover(Usuarios u) {
        usuariosService.remover(u);
        listaUsuarios = usuariosService.listarTodos();
        u = new Usuarios();
    }

    /**
     * 
     * @param u
     */
    public void editar(Usuarios u) {
        this.usuarios = u;
        edicao = true;
    }

    public List<Usuarios> getListaUsuarios() {
        return listaUsuarios;
    }

    public void setListaUsuarios(List<Usuarios> listaUsuarios) {
        this.listaUsuarios = listaUsuarios;
    }

    public Usuarios getUsuario() {
        return usuarios;
    }

    public void setUsuario(Usuarios usuario) {
        this.usuarios = usuario;
    }

    public boolean isEdicao() {
        return edicao;
    }

    public void setEdicao(boolean edicao) {
        this.edicao = edicao;
    }

}

Dao:

package br.com.sicoob.DAO;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import br.com.sicoob.Service.UsuariosService;
import br.com.sicoob.entidades.Usuarios;

@Stateless
public class UsuariosDAO implements Serializable, UsuariosService {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;


    @PersistenceContext
    private EntityManager em;

    /**
     * 
     * @param u
     */
    @Override
    public void remover(Usuarios u) {
        u = em.merge(u);
        em.remove(u);
    }

    /**
     * 
     * @return
     */
    @Override
    @SuppressWarnings("unchecked")
    public List<Usuarios> listarTodos() {
        List<Usuarios> usuarios = new ArrayList<Usuarios>();
        Query q = em.createQuery("select obj from Usuarios obj");
        usuarios = q.getResultList();
        return usuarios;
    }

    /**
     * 
     * @param id
     * @return
     */
    public Usuarios buscarPorId(Long id) {
        return em.find(Usuarios.class, id);
    }

    /**
     * 
     * @param u
     * @return
     */
    public Usuarios carregarEntidade(Usuarios u) {
        return em.merge(u);
    }

    @Override
    public void salvarUsuario(Usuarios usuario) {
        if(usuario.getId() == null) {
            em.persist(usuario);
        } else {
            em.merge(usuario);
        }

    }

}

Entity:

package br.com.sicoob.entidades;


import javax.persistence.*;

import java.io.Serializable;
import java.util.Date;


/**
 * The persistent class for the usuarios database table.
 * 
 */
@Entity
@Table(name="usuarios")
@TableGenerator(name = "usuarios", table = "id", pkColumnName = "nome", valueColumnName = "gen_val", allocationSize = 1, pkColumnValue = "employee_gen")
@NamedQuery(name="Usuarios.findAll", query="SELECT u FROM Usuarios u")
public class Usuarios implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private String id;

    @Column(name="nome")
    private String nome;

    @Column(name="email")
    private String email;

    @Column(name="telefone")
    private String telefone;

    @Temporal(TemporalType.DATE)
    private Date dataNascimento;

    String login;
    String senha;


    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getDataNascimento() {
        return dataNascimento;
    }

    public void setDataNascimento(Date dataNascimento) {
        this.dataNascimento = dataNascimento;
    }

    public String getTelefone() {
        return telefone;
    }

    public void setTelefone(String telefone) {
        this.telefone = telefone;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((email == null) ? 0 : email.hashCode());
        result = prime * result + ((nome == null) ? 0 : nome.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Usuarios other = (Usuarios) obj;
        if (email == null) {
            if (other.email != null)
                return false;
        } else if (!email.equals(other.email))
            return false;
        if (nome == null) {
            if (other.nome != null)
                return false;
        } else if (!nome.equals(other.nome))
            return false;
        return true;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getSenha() {
        return senha;
    }

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

}

Service:

package br.com.sicoob.Service;

import java.util.List;

import br.com.sicoob.entidades.Usuarios;

public interface UsuariosService {
    public void salvarUsuario(Usuarios usuario);

    public List<Usuarios> listarTodos();

    public void remover(Usuarios u);
}
  • In the debugger, at what point does the error occur? Use multiple Brake points to check.

  • 1

    This all mixed, first that you are in a Javaee7 environment with native CDI and uses JSF managedBean, forget this @Managedbean and use @Named , not to be arrogant but rather constructive need to understand certain patterns, one of them is EJB3, what should be a repository is a service implementation and this wrong, service interface is @Local , impl is @Stateless and the repository is dependent on CDI.

  • Swap the @EJB of the controllers by @Inject not that it is wrong but it matches everything above, see if you have the necessary libs of Hibernate as Preview, use the (GOOD) Billing of Materials.

  • Subistitui EJB by @Inject and created the Service classes to bridge the MB call using Inject to call the service inside the MB, I will test and return.

No answers

Browser other questions tagged

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