Olá a todos.
Estou com o seguinte problema: Tenho uma classe Pessoa e uma classe Funcionario que herda Pessoa. O meu problema surge quando tento inserir um funcionário. Acredito que o problema seja minha DAO. Segue abaixo minhas modelos e DAOs de Pessoa e Funcionário.
Modelo Pessoa
public class Pessoa {
private int idPessoa;
private String cnpPessoa;
private String nomePessoa;
private String nomeRedPessoa;
private TipoPessoa tipoPessoa;
private Date dtNascPessoa;
private Genero generoPessoa;
private String observacaoPessoa;
private Status statusPessoa;
public Pessoa() {
}
public Pessoa(int idPessoa, String cnpPessoa, String nomePessoa, String nomeRedPessoa, TipoPessoa tipoPessoa, Date dtNascPessoa, Genero generoPessoa, String observacaoPessoa, Status statusPessoa) {
this.idPessoa = idPessoa;
this.cnpPessoa = cnpPessoa;
this.nomePessoa = nomePessoa;
this.nomeRedPessoa = nomeRedPessoa;
this.tipoPessoa = tipoPessoa;
this.dtNascPessoa = dtNascPessoa;
this.generoPessoa = generoPessoa;
this.observacaoPessoa = observacaoPessoa;
this.statusPessoa = statusPessoa;
}
/**
* @return the idPessoa
*/
public int getIdPessoa() {
return idPessoa;
}
/**
* @param idPessoa the idPessoa to set
*/
public void setIdPessoa(int idPessoa) {
this.idPessoa = idPessoa;
}
/**
* @return the cnpPessoa
*/
public String getCnpPessoa() {
return cnpPessoa;
}
/**
* @param cnpPessoa the cnpPessoa to set
*/
public void setCnpPessoa(String cnpPessoa) {
this.cnpPessoa = cnpPessoa;
}
/**
* @return the nomePessoa
*/
public String getNomePessoa() {
return nomePessoa;
}
/**
* @param nomePessoa the nomePessoa to set
*/
public void setNomePessoa(String nomePessoa) {
this.nomePessoa = nomePessoa;
}
/**
* @return the nomeRedPessoa
*/
public String getNomeRedPessoa() {
return nomeRedPessoa;
}
/**
* @param nomeRedPessoa the nomeRedPessoa to set
*/
public void setNomeRedPessoa(String nomeRedPessoa) {
this.nomeRedPessoa = nomeRedPessoa;
}
/**
* @return the tipoPessoa
*/
public TipoPessoa getTipoPessoa() {
return tipoPessoa;
}
/**
* @param tipoPessoa the tipoPessoa to set
*/
public void setTipoPessoa(TipoPessoa tipoPessoa) {
this.tipoPessoa = tipoPessoa;
}
/**
* @return the dtNascPessoa
*/
public Date getDtNascPessoa() {
return dtNascPessoa;
}
/**
* @param dtNascPessoa the dtNascPessoa to set
*/
public void setDtNascPessoa(Date dtNascPessoa) {
this.dtNascPessoa = dtNascPessoa;
}
/**
* @return the generoPessoa
*/
public Genero getGeneroPessoa() {
return generoPessoa;
}
/**
* @param generoPessoa the generoPessoa to set
*/
public void setGeneroPessoa(Genero generoPessoa) {
this.generoPessoa = generoPessoa;
}
/**
* @return the observacaoPessoa
*/
public String getObservacaoPessoa() {
return observacaoPessoa;
}
/**
* @param observacaoPessoa the observacaoPessoa to set
*/
public void setObservacaoPessoa(String observacaoPessoa) {
this.observacaoPessoa = observacaoPessoa;
}
/**
* @return the statusPessoa
*/
public Status getStatusPessoa() {
return statusPessoa;
}
/**
* @param statusPessoa the statusPessoa to set
*/
public void setStatusPessoa(Status statusPessoa) {
this.statusPessoa = statusPessoa;
}
}
Modelo Funcionario
public class Funcionario extends Pessoa {
private Integer idFuncionario;
public Funcionario() {
}
public Funcionario(Integer idFuncionario) {
this.idFuncionario = idFuncionario;
}
public Funcionario(Integer idFuncionario, int idPessoa, String cnpPessoa, String nomePessoa, String nomeRedPessoa, TipoPessoa tipoPessoa, Date dtNascPessoa, Genero generoPessoa, String observacaoPessoa, Status statusPessoa) {
super(idPessoa, cnpPessoa, nomePessoa, nomeRedPessoa, tipoPessoa, dtNascPessoa, generoPessoa, observacaoPessoa, statusPessoa);
this.idFuncionario = idFuncionario;
}
/**
* @return the idFuncionario
*/
public Integer getIdFuncionario() {
return idFuncionario;
}
/**
* @param idFuncionario the idFuncionario to set
*/
public void setIdFuncionario(Integer idFuncionario) {
this.idFuncionario = idFuncionario;
}
}
DAO Pessoa
public class PessoaDAO {
private Connection conexao;
public PessoaDAO() throws Exception {
try {
this.conexao = ConnectionFactory.getConnection();
System.out.println("Conectado com sucesso.");
} catch (Exception ex) {
throw new Exception(ex.getMessage());
}
}
public Integer inserir(Pessoa pessoa) {
PreparedStatement pstm = null;
ResultSet rs = null;
Integer idPessoa = null;
String sql = "INSERT INTO pessoa (cnp_pessoa, nome_pessoa, nomered_pessoa, tipo_pessoa, "
+ "dtnasc_pessoa, genero_pessoa, observacao_pessoa, status_pessoa) "
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?) RETURNING id_pessoa ;";
try {
pstm = conexao.prepareStatement(sql);
pstm.setString(1, pessoa.getCnpPessoa());
pstm.setString(2, pessoa.getNomePessoa());
pstm.setString(3, pessoa.getNomeRedPessoa());
pstm.setInt(4, pessoa.getTipoPessoa().getIdTipoPessoa());
pstm.setDate(5, new java.sql.Date(pessoa.getDtNascPessoa().getTime()));
pstm.setString(6, pessoa.getObservacaoPessoa());
pstm.setInt(7, pessoa.getGeneroPessoa().getIdGenero());
pstm.setInt(8, pessoa.getStatusPessoa().getIdStatus());
rs = pstm.executeQuery();
while (rs.next()) {
idPessoa = rs.getInt("id_pessoa");
}
} catch (SQLException ex) {
System.out.println("Erro na DAO ao inserir Pessoa. Erro: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
ConnectionFactory.closeConnection(conexao, pstm, rs);
} catch (Exception ex) {
System.out.println("Erro ao fechar os parametros de conexao. Erro: " + ex.getMessage());
ex.printStackTrace();
}
}
return idPessoa;
}
}
DAO Funcionario
public class FuncionarioDAOImp implements GenericDAO {
private Connection conexao;
public FuncionarioDAOImp() throws Exception {
try {
this.conexao = ConnectionFactory.getConnection();
System.out.println("Conectado com sucesso.");
} catch (Exception ex) {
throw new Exception(ex.getMessage());
}
}
@Override
public boolean inserir(Object object) {
Funcionario funcionario = (Funcionario) object;
PreparedStatement pstm = null;
String sql = "INSERT INTO funcionario(pessoa) VALUES (?);";
try {
pstm = conexao.prepareStatement(sql);
try {
PessoaDAO daoPessoa = new PessoaDAO();
pstm.setInt(1, daoPessoa.inserir(funcionario));
} catch (Exception ex) {
System.out.println("Erro na DAO Funcionario ao inserir Pessoa. Erro: " + ex.getMessage());
ex.printStackTrace();
}
pstm.execute();
return true;
} catch (SQLException ex) {
System.out.println("Erro na DAO ao inserir Funcionario. Erro: " + ex.getMessage());
ex.printStackTrace();
return false;
} finally {
try {
ConnectionFactory.closeConnection(conexao, pstm);
} catch (Exception ex) {
System.out.println("Erro ao fechar os parametros de conexao. Erro: " + ex.getMessage());
ex.printStackTrace();
}
}
}
@Override
public Object recuperar(int idObject) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public boolean editar(Object object) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void excluir(int idObject) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public List<Object> listar() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
Servlet InsereFuncionario
public class InserirFuncionario extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String mensagemSalvar = null;
String cnpPessoa = request.getParameter("cnpPessoa");
String nomePessoa = request.getParameter("nomePessoa");
String nomeRedPessoa = request.getParameter("nomeRedPessoa");
Integer idTipoPessoa = Integer.parseInt(request.getParameter("tipoPessoa"));
Date dtNascPessoa = Conversoes.converterData(request.getParameter("dtNascPessoa").replace("-", "/"));
Integer idGenero = Integer.parseInt(request.getParameter("generoPessoa"));
String observacaoPessoa = request.getParameter("observacaoPessoa");
Integer idStatus = Integer.parseInt(request.getParameter("statusPessoa"));
if (cnpPessoa.isEmpty() || "".equals(cnpPessoa)) {
response.sendRedirect("DadosInserirFuncionario?status=erroValidacao");
} else if (nomePessoa.isEmpty() || "".equals(nomePessoa)) {
response.sendRedirect("DadosInserirFuncionario?status=erroValidacao");
} else {
Funcionario funcionario = new Funcionario();
funcionario.setCnpPessoa(cnpPessoa);
funcionario.setNomePessoa(nomePessoa);
funcionario.setNomeRedPessoa(nomeRedPessoa);
TipoPessoa tipopessoa = new TipoPessoa();
tipopessoa.setIdTipoPessoa(idTipoPessoa);
funcionario.setTipoPessoa(tipopessoa);
funcionario.setDtNascPessoa(dtNascPessoa);
Genero genero = new Genero();
genero.setIdGenero(idGenero);
funcionario.setGeneroPessoa(genero);
funcionario.setObservacaoPessoa(observacaoPessoa);
Status status = new Status();
status.setIdStatus(idStatus);
funcionario.setStatusPessoa(status);
try {
GenericDAO dao = new FuncionarioDAOImp();
if (dao.inserir(funcionario)) {
mensagemSalvar = "OK";
} else {
mensagemSalvar = "ERRO";
}
request.setAttribute("mensagemSalvar", mensagemSalvar);
request.getRequestDispatcher("DadosInserirFuncionario").forward(request, response);
} catch (Exception ex) {
System.out.println("Erro na servlet ao inserir Funcionario. Erro: " + ex.getMessage());
ex.printStackTrace();
}
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
ERRO que aparece:
Informações: Erro na DAO ao inserir Pessoa. Erro: ERROR: column "genero_pessoa" is of type integer but expression is of type character varying
Dica: You will need to rewrite or cast the expression.
Posição: 167
Informações: Erro na DAO Funcionario ao inserir Pessoa. Erro: null
Informações: Erro na DAO ao inserir Funcionario. Erro: Nenhum valor especificado para parâmetro 1.
Grave: org.postgresql.util.PSQLException: ERROR: column "genero_pessoa" is of type integer but expression is of type character varying
Dica: You will need to rewrite or cast the expression.
Posição: 167
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2310)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2023)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:217)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:421)
at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:166)
at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:118)
at br.com.pollymodas.DAO.PessoaDAO.inserir(PessoaDAO.java:49)
at br.com.pollymodas.DAO.FuncionarioDAOImp.inserir(FuncionarioDAOImp.java:42)
at br.com.pollymodas.control.InserirFuncionario.processRequest(InserirFuncionario.java:72)
at br.com.pollymodas.control.InserirFuncionario.doPost(InserirFuncionario.java:112)
Galera, se alguém puder me ajudar, agradeço muito.