FJ21 - Exemplo Hibernate não funciona [novato]

3 respostas
P

Boa noite, Pessoal!

Estou tentando fazer o primeiro exemplo do curso FJ21, mas está acontecendo o erro abaixo, agradeceria muito de alguém pudesse me ajudar.
Fiz uma pesquisa, até achei uma pessoa com problema no exercício 13.7, mas o erro é diferente do meu...

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version). log4j:WARN Please initialize the log4j system properly. 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:289) at org.hibernate.dialect.Dialect.getDialect(Dialect.java:267) at org.hibernate.dialect.Dialect.getDialect(Dialect.java:282) at org.hibernate.tool.hbm2ddl.SchemaExport.(SchemaExport.java:121) at org.hibernate.tool.hbm2ddl.SchemaExport.(SchemaExport.java:91) at br.com.paresque.hibernate.GeraTabelas.main(GeraTabelas.java:12)

As duas classes utilizadas:

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

public class GeraTabelas {
	public static void main(String[] args) {
		AnnotationConfiguration cfg = new AnnotationConfiguration();
		cfg.addAnnotatedClass(Produto.class);
		
		SchemaExport se = new SchemaExport(cfg);
		se.create(true, true);
	}	
}
import java.util.Calendar;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Produto {
	
	@Id
	@GeneratedValue
	private Long id;
	private String nome;
	private double preco;
	private Calendar dataInicioVenda;
	//getters e setters abaixo
}

Arquivos .properties como informado na apostila:

hibernate.dialect = org.hibernate.dialect.MySQLInnoDBDialect

hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://localhost/fj21
hibernate.connection.username = root
hibernate.connection.password =

hibernate.show_sql = true
hibernate.formate_sql = true


log4j.appender.stdout=org.apache.log4.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattner=%d{HH:mm:ss} %5p [%c{1}] %m%n

log4j.rootLogger=warn, stdout
log4j.logger.org.hibernate=info

Agradeço muito!

3 Respostas

renanigt

Cara, tenta fazer assim no teu código:

import org.hibernate.cfg.AnnotationConfiguration;  
    import org.hibernate.tool.hbm2ddl.SchemaExport;  
      
    public class GeraTabelas {  
        public static void main(String[] args) {  
            AnnotationConfiguration cfg = new AnnotationConfiguration();  
            cfg.addAnnotatedClass(Produto.class);
             cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect")
              
            SchemaExport se = new SchemaExport(cfg);  
           se.create(true, true);  
       }     
}

Só adicionar cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect") !

P

renanigt:
Cara, tenta fazer assim no teu código:

Só adicionar cfg.setProperty(“hibernate.dialect”, “org.hibernate.dialect.MySQLInnoDBDialect”) !

Valeu, renanigt!

Na verdade ele não está mais gerando a exception. Mas ele não criou a tabela no banco de dados :cry:

Estou usando os seguintes jars:

  • antlr-2.7.6.jar
  • commons-collections-3.1.jar
  • dom4j-1.6.1.jar
  • hibernate3.jar
  • hibernate-jpa-2.0-api-1.0.0.Final.jar
  • javassist-3.9.0.GA.jar
  • jta-1.1.jar
  • slf4j-api-1.5.8.jar
  • slf4j-log4j12-1.5.8.jar
  • log4j-1.2.15.jar
  • mysql-connector-java-5.1.12-bin.jar

Todos foram colocados no build path.
Alguma ideia ? :?:

Obrigado!

renanigt

Cara, posso estar enganado, mas acho q na classe GeraTabelas, todas as propriedades do hibernate vc vai ter q fazer tipo:

Criado 29 de maio de 2010
Ultima resposta 30 de mai. de 2010
Respostas 3
Participantes 2