Erro hibernate

4 respostas
rodrigoaramburu

Pessoal estava tentando seguir um tutorial para começar a utilizar o hibernate mas esta dando o seguinte erro

08/01/2010 09:55:11 org.hibernate.cfg.annotations.Version <clinit>
INFO: Hibernate Annotations 3.3.1.GA
08/01/2010 09:55:11 org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.5
08/01/2010 09:55:11 org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
08/01/2010 09:55:11 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
08/01/2010 09:55:11 org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
Exception in thread "main" org.hibernate.HibernateException: The dialect was not set. Set the property hibernate.dialect.
        at org.hibernate.dialect.Dialect.instantiateDialect(Dialect.java:233)
        at org.hibernate.dialect.Dialect.getDialect(Dialect.java:211)
        at org.hibernate.dialect.Dialect.getDialect(Dialect.java:226)
        at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:86)
        at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:61)
        at pessoas.Teste.main(Teste.java:19)
Java Result: 1

andei dando uma pesquisada já tentei trocar o dialect para “org.hibernate.dialect.MySQLDialect”, “org.hibernate.dialect.MySQLInnoDBDialect” e “org.hibernate.dialect.MySQLMyISAMDialect” e não funcionou.

Segue meu código

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost/pessoas</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">123456</property>
  </session-factory>
</hibernate-configuration>
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pessoas;

import javax.persistence.*;


/**
 *
 * @author g8si
 */
@Entity
public class Pessoa {
    @Id
    @GeneratedValue
    private int id;
    private String nome;
    private int idade;

    public int getId() {
        return id;
    }

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

    public int getIdade() {
        return idade;
    }

    public void setIdade(int idade) {
        this.idade = idade;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pessoas;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

/**
 *
 * @author g8si
 */
public class Teste {
    public static void main(String args[]){
        AnnotationConfiguration cfg = new AnnotationConfiguration();
        cfg.addAnnotatedClass(Pessoa.class);
        SchemaExport se = new SchemaExport(cfg);
        se.create(true, true);
    }
}

As bibliotecas que importei são as seguintes

4 Respostas

C

Bom dia

Então acredito que o erro esteja na sua classe de execução faça o seguinte:

1º) No arquivo hibernate.cfg.xml adicione a seguintes linhas:

<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="pacote.Pessoa" />

hibernate.hbm2ddl.auto ==>> significa que o hibernate deve gerar a base de dados assim que o arquivo cfg for carregado em memória, com a opção update assim que arquivo for carregado, as tabelas são criadas em caso delas não existir.

2º) Na classe de execução troque por esse trecho de código:

public class Teste {     
   public static void main(String args[]){  
        AnnotationConfiguration cfg = new AnnotationConfiguration();  
        cgf.configure();
     }  
 }

Acredito que assim seu código já irá funcionar, caso tenha algum outro problema poste sua dúvida.

Falou.

rodrigoaramburu

Não deu erro nenhum, mas também não criou a tabela

C

Bom dia

Então blz, faça assim agora e vamos ver se ele cria a tabela:

public class Teste {       
      public static void main(String args[]){    
          AnnotationConfiguration cfg = new AnnotationConfiguration();    
          cgf.configure();  

         SessionFactory sf = cfg.buildSessionFactory();
 
        Session session = sf.openSession();

         Transaction tx = session.beginTransaction();
    		
         Pessoa pessoa = new Pessoa();
 
         //Adicione os valores as métodos setters
         
    	 session.save(pessoa);
    		
    	  tx.commit();

          session.close();

         sf.close();

      }    
   }

Outra coisa na sua classe pessoa no @GenerateValue, adiciona a strategy de geração de id, coloque o seguinte:

@GenerateValue(strategy=GenerationType.IDENTITY)

Teste e poste o resultado.

Falou.

rodrigoaramburu

Value, funcionou beleza.

Muito Obrigado.

To gostando desse tal de Hibernate.

Criado 8 de janeiro de 2010
Ultima resposta 8 de jan. de 2010
Respostas 4
Participantes 2