Error inserting data with Hibernate

Asked

Viewed 64 times

1

I’m having an error inserting into my database:

ERROR: You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near 'de Admissao, Data de Demissao, Nome, Estado Civil, Sexo, CEP, CPF, RG, Data de N' at line 1 Exception in thread "AWT-Eventqueue-0" org.hibernate.Exception.Sqlgrammarexception: could not execute statement.

This is 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’s 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 is 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 from 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();
  • The name of the columns in the database are just like this, with spaces such as "Date of Admission"?

1 answer

1


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

I mean, 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 that my other answer to understand why.

  • Our guy worked worked did not know these woes of the ` in the columns I thought I was going without even now the rest as it is technical course final project is enough for now to do this way

  • @Douglaslopes If my answer has served you and there is no doubt left, I ask you to mark it as accepted/correct by clicking on the green next to it, below the scoring/voting numbers. Thank you.

Browser other questions tagged

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