Error inserting data with Hibernate

1

I'm having an error inserting into my database:

  

ERROR: You have an error in your SQL syntax; Admission, Date of Resignation, Name, Marital Status, Sex, Zip Code, CPF, RG, Date of N 'at line 1   Exception in thread "AWT-EventQueue-0" org.hibernate.exception.SQLGrammarException: could not execute statement.

Here's my POJO:

public class Funcionario implements Serializable {



    private Integer iDFuncionario;
    private Date datadeAdmissao;
    private Date 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) {
      this.nome=nome;

    }


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

    public Integer getiDFuncionario() {
        return iDFuncionario;
    }


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

    public Date getDatadeAdmissao() {
        return datadeAdmissao;
    }

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

    public Date getDatadeDemissao() {
        return datadeDemissao;
    }

    public void setDatadeDemissao(Date 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 + " ]";
    }

}

Here is my class mapping:

<?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="date" length ="50" column="Data de Admissao" />
        <property name="datadeDemissao" type="date" 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>

Here's my class HibernateUtil :

package Hibernate;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;




/**
 * Hibernate Utility class with a convenient method to get Session Factory
 * object.
 *
 * @author Douglas
 */
public class HibernateUtil {

   private static SessionFactory sessionFactory;

    public static SessionFactory getSessionFactory() {
        if (sessionFactory == null) {
            // loads configuration and mappings
            Configuration configuration = new Configuration().configure("Hibernate/hibernate.cfg.xml");
            ServiceRegistry serviceRegistry
                = new StandardServiceRegistryBuilder()
                    .applySettings(configuration.getProperties()).build();

            // builds a session factory from the service registry
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);           
        }

        return sessionFactory;
    }
}

And here, part of the code on my employee screen:

private String nome;


 String nome=Txt_Nome.getText();



      SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

      Session session = sessionFactory.openSession();


      Funcionario f1=new Funcionario(nome);




      session.beginTransaction();
      session.save(f1);
      session.getTransaction().commit();
      session.close();
    
asked by anonymous 07.01.2017 / 20:21

1 answer

1

The problem seems to be the spaces in the column names. If your database contains columns with spaces in the names, use the ' to escape them.

That is, your XML looks like this:

<?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="date" length ="50" column="'Data de Admissao'" />
        <property name="datadeDemissao" type="date" 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>

Also, I see that you are manipulating the database from within the dedicated AWT / Swing thread. This is not a good thing to do. See this my other answer to understand why.

    
08.01.2017 / 02:29