Error searching for data in Jtable with Hibernate

Asked

Viewed 120 times

0

The following error of when searching the data:

 INFO: HHH000182: No default (no-argument) constructor for class:
 Classes.Funcionario (class must be instantiated by Interceptor)

 Hibernate: select funcionari0_.ID_Funcionario as ID_Funci1_0_,
 funcionari0_.`Data de Admissao` as Data2_0_, funcionari0_.`Data de
 Demissao` as Data3_0_, funcionari0_.Nome as Nome4_0_,
 funcionari0_.`Estado Civil` as Estado5_0_, funcionari0_.Sexo as
 Sexo6_0_, funcionari0_.CEP as CEP7_0_, funcionari0_.CPF as CPF8_0_,
 funcionari0_.RG as RG9_0_, funcionari0_.`Data de Nascimento` as
 Data10_0_, funcionari0_.Estado as Estado11_0_, funcionari0_.Cidade as
 Cidade12_0_, funcionari0_.Bairro as Bairro13_0_, funcionari0_.Endereco
 as Enderec14_0_, funcionari0_.TEL as TEL15_0_, funcionari0_.CEL as
 CEL16_0_, funcionari0_.Email as Email17_0_, funcionari0_.Cargo as
 Cargo18_0_, funcionari0_.`Nivel de Escolaridade` as Nivel19_0_ from
 funcionario funcionari0_ 

Exception in thread "AWT-EventQueue-0" org.hibernate.InstantiationException: No default constructor for entity:  : Classes.Funcionario

JTABLE’S CANVAS

package Telas;

import Classes.Funcionario;
import Hibernate.HibernateUtil;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;


 */
public class Tela_ConsultarFuncionario extends javax.swing.JInternalFrame {


    List <Funcionario> funcionarios  = new ArrayList<>();

    String [] Colunas = new String [] {"nome,datadeAdmissao,estadoCivil,sexo,cep,cpf,rg,datadeNascimento,estado,cidade,bairro,endereco,tel,cel,email,cargo,niveldeEscolaridade"};
    String [] [] Dados = new String [10] [16];



    public Tela_ConsultarFuncionario() {


        initComponents();

        Tbl_Funcionarios.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

}                    


    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:

        String [][] Dados = new String [funcionarios.size()] [17];

        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

        Session session = sessionFactory.openSession();

      Query query = session.createQuery("select c from Funcionario c");
       funcionarios = query.list();

         int i = 0;



          String [] Colunas = new String[]{"nome,datadeAdmissao,estadoCivil,sexo,cep,cpf,rg,datadeNascimento,estado,cidade,bairro,endereco,tel,cel,email,cargo,niveldeEscolaridade"};


         while( i<funcionarios.size() )
        {

            Dados [i][0] = funcionarios.get(i).getDatadeAdmissao();
            Dados [i][1] = funcionarios.get(i).getNome();
            Dados [i][2] = funcionarios.get(i).getDatadeNascimento();
            Dados [i][3] = funcionarios.get(i).getSexo();
            Dados [i][4] = funcionarios.get(i).getEstadoCivil();
            Dados [i][5] = funcionarios.get(i).getCep();
            Dados [i][6] = funcionarios.get(i).getCpf();
            Dados [i][7] = funcionarios.get(i).getRg();
            Dados [i][8] = funcionarios.get(i).getEstado();
            Dados [i][9] = funcionarios.get(i).getCidade();
            Dados [i][10] = funcionarios.get(i).getBairro();
            Dados [i][11] = funcionarios.get(i).getEndereco();
            Dados [i][12] = funcionarios.get(i).getTel();
            Dados [i][13] = funcionarios.get(i).getCel();
            Dados [i][14] = funcionarios.get(i).getEmail();
            Dados [i][15] = funcionarios.get(i).getNiveldeEscolaridade();
            Dados [i][16] = funcionarios.get(i).getCargo();

          i++;

        }


        session.beginTransaction();
        session.getTransaction().commit();
        session.close();


        DefaultTableModel modelo = new DefaultTableModel(Dados,Colunas);
       Tbl_Funcionarios.setModel( modelo );  





    }                                        


}

STAFF SCREEN

package Classes;

import java.io.Serializable;


public class Funcionario implements Serializable {



    private Integer iDFuncionario;
    private String datadeAdmissao;
    private String datadeDemissao;
    private String nome;
    private String estadoCivil;
    private String sexo;
    private String cep;
    private String cpf;
    private String rg;
    private String datadeNascimento;
    private String estado;
    private String cidade;
    private String bairro;
    private String endereco;
    private String tel;
    private String cel;
    private String email;
    private String cargo;
    private String niveldeEscolaridade;

    public Funcionario(String nome,String datadeAdmissao,String datadeDemissao,String estadoCivil,String sexo,String cep,String cpf,String rg,String datadeNascimento,String estado
    ,String cidade,String bairro,String endereco,String tel,String cel,String email,String cargo,String niveldeEscolaridade) {
      this.nome=nome;
      this.datadeAdmissao=datadeAdmissao;
      this.datadeDemissao=datadeDemissao;
      this.estadoCivil=estadoCivil;
      this.sexo=sexo;
      this.cep=cep;
      this.cpf=cpf;
      this.rg=rg;
      this.datadeNascimento=datadeNascimento;
      this.estado=estado;
      this.cidade=cidade;
      this.bairro=bairro;
      this.endereco=endereco;
      this.tel=tel;
      this.cel=cel;
      this.email=email;
      this.cargo=cargo;
      this.niveldeEscolaridade=niveldeEscolaridade;
    }

    public Funcionario(Integer iDFuncionario) {
     this.iDFuncionario=iDFuncionario;
    }



    public Integer getiDFuncionario() {
        return iDFuncionario;
    }


    public void setiDFuncionario(Integer iDFuncionario) {
        this.iDFuncionario = iDFuncionario;
    }

    public String getDatadeAdmissao() {
        return datadeAdmissao;
    }

    public void setDatadeAdmissao(String datadeAdmissao) {
        this.datadeAdmissao = datadeAdmissao;
    }

    public String getDatadeDemissao() {
        return datadeDemissao;
    }

    public void setDatadeDemissao(String datadeDemissao) {
        this.datadeDemissao = datadeDemissao;
    }

    public String getNome() {
        return nome;
    }

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

    public String getEstadoCivil() {
        return estadoCivil;
    }

    public void setEstadoCivil(String estadoCivil) {
        this.estadoCivil = estadoCivil;
    }

    public String getSexo() {
        return sexo;
    }

    public void setSexo(String sexo) {
        this.sexo = sexo;
    }

    public String getCep() {
        return cep;
    }

    public void setCep(String cep) {
        this.cep = cep;
    }

    public String getCpf() {
        return cpf;
    }

    public void setCpf(String cpf) {
        this.cpf = cpf;
    }

    public String getRg() {
        return rg;
    }

    public void setRg(String rg) {
        this.rg = rg;
    }

    public String getDatadeNascimento() {
        return datadeNascimento;
    }

    public void setDatadeNascimento(String datadeNascimento) {
        this.datadeNascimento = datadeNascimento;
    }

    public String getEstado() {
        return estado;
    }

    public void setEstado(String estado) {
        this.estado = estado;
    }

    public String getCidade() {
        return cidade;
    }

    public void setCidade(String cidade) {
        this.cidade = cidade;
    }

    public String getBairro() {
        return bairro;
    }

    public void setBairro(String bairro) {
        this.bairro = bairro;
    }

    public String getEndereco() {
        return endereco;
    }

    public void setEndereco(String endereco) {
        this.endereco = endereco;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String getCel() {
        return cel;
    }

    public void setCel(String cel) {
        this.cel = cel;
    }

    public String getEmail() {
        return email;
    }

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

    public String getCargo() {
        return cargo;
    }

    public void setCargo(String cargo) {
        this.cargo = cargo;
    }

    public String getNiveldeEscolaridade() {
        return niveldeEscolaridade;
    }

    public void setNiveldeEscolaridade(String niveldeEscolaridade) {
        this.niveldeEscolaridade = niveldeEscolaridade;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (iDFuncionario != null ? iDFuncionario.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Funcionario)) {
            return false;
        }
        Funcionario other = (Funcionario) object;
        if ((this.iDFuncionario == null && other.iDFuncionario != null) || (this.iDFuncionario != null && !this.iDFuncionario.equals(other.iDFuncionario))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Classes.Funcionario[ iDFuncionario=" + iDFuncionario + " ]";
    }

}

EMPLOYEE MAPPING SCREEN

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="Classes.Funcionario" table="funcionario" >
  <id name="iDFuncionario" column="ID_Funcionario">
      <generator class="native"/>
  </id>
        <property name="datadeAdmissao" type="string" length ="50" column="`Data de Admissao`" />
        <property name="datadeDemissao" type="string" length ="50" column="`Data de Demissao`"/>
        <property name="nome" type="string" length="50" column="Nome"/>
        <property name="estadoCivil" type="string" length="50" column="`Estado Civil`"/>
        <property name="sexo" type="string" length ="50" column="Sexo"/>
        <property name="cep" type="string" length ="50" column="CEP"/>
        <property name="cpf" type="string" length ="50" column="CPF"/>
        <property name="rg" type="string" length ="50" column="RG"/>
        <property name="datadeNascimento" type="string" length ="50" column="`Data de Nascimento`"/>
        <property name="estado" type="string" length ="50" column="Estado"/>
        <property name="cidade" type="string" length ="50" column="Cidade"/>
        <property name="bairro" type="string" length ="50" column="Bairro" />
        <property name="endereco" type="string" length ="50" column="Endereco"/>
        <property name="tel" type="string" length ="50" column="TEL"/>
        <property name="cel" type="string" length ="50" column="CEL"/>
        <property name="email" type="string" length ="50" column="Email"/>
        <property name="cargo" type="string" length ="50" column="Cargo"/>
        <property name="niveldeEscolaridade" type="string" length ="50" column="`Nivel de Escolaridade`"/>
    </class>
</hibernate-mapping>
  • Add your working class.

  • how ?

  • I meant to add the class to the question, this class is involved in the error as well.

  • Ah ta, I didn’t understand kkkkk

  • 1

    I don’t know Ibernate, but the error seems to be related to lack of a default constructor in the case, public Funcionario(){...}

  • Tip, don’t snipet for java code, just paste the code, select it and click {}

  • Blz, change some, it’s better this way ?

  • From what I read in Soen, Hibernate requires that you have a default constructor, that is, the one without arguments as I exemplified in the previous comment, because it uses reflection to create the bean. First it creates an empty object and then fills it with getters and setters as needed. When you customize the constructor with arguments, the default constructor needs to be explicitly implemented in the class.

  • ah, I’ll see you move it here to see

  • Exception in thread "AWT-Eventqueue-0" java.lang.Arrayindexoutofboundsexception: 0

  • now changed the error

  • This is already another problem, which would require another different question. the error of the question was solved with the last hint. I converted as a response to improve the visibility of the tip to other people.

  • Do not forget to quote this question in the new, to facilitate the analysis of who answer :)

  • Just create another question then ?

  • Yes, and add the full error stack as you did in this.

Show 10 more comments

1 answer

1


In accordance with this reply from Soen, Hibernate requires you to have a default constructor, that is, the one without arguments as I exemplified in the comments, because it uses Reflection to create the bean. First it creates an empty object and then fills it with the setters as needed. When you customize the constructor with arguments, the default constructor needs to be explicitly implemented in the class.

I believe that adding a standard constructor of the form below solves the error:

public Funcionario(){ 

}

Browser other questions tagged

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