0
Geratable class with the main method
package br.com.drogaria.main;
import br.com.drogaria.util.HibernateUtil;
public class GeraTabela {
public static void main(String[] args) {
HibernateUtil.getSessionFactory();
System.out.println();
HibernateUtil.getSessionFactory().close();
System.out.println("Seção é encerrada pelo método HibernateUtil.getSessionFactory().close();");
}
}
The Hibernateutil class that interacts with Hibernate.cfg.xml
package br.com.drogaria.util;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
Configuration configuration = new Configuration();
configuration.configure();
System.out.println("configuração criada!");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
System.out.println("hibernate.cfg.xml registrado!");
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
System.out.println("Seção Criada!");
System.out.println("método buildSessionFactory() retorna uma sessionFactory a partir do hibernate.cfg.xml");
return sessionFactory;
} catch (Throwable ex) {
// Exibe uma mensagem de eerro no console
throw new ExceptionInInitializerError(ex);
}
}
// public class GeraTabela invoca HibernateUtil.getSessionFactory();
public static SessionFactory getSessionFactory() {
System.out.println("GeraTabela.java invoca o método HibernateUtil.getSessionFactory() para obter uma sessionFactory()");
return sessionFactory;
}
}
The configuration file Hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Configurações da conexão -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/db_drogaria</property>
<property name="connection.username">root</property>
<property name="connection.password">123salmir</property>
<!-- Pool de conexões JDBC -->
<property name="connection.pool_size">1</property>
<!-- Dialeto SQL -->
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- Gerenciamento da sessão -->
<property name="current_session_context_class">thread</property>
<!-- Desabilita a cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Exibe os comandos SQL -->
<property name="show_sql">true</property>
<!--Cria as tabelas -->
<property name="hbm2dd1.auto">update</property>
<mapping class="br.com.drogaria.domain.Fabricante" />
</session-factory>
</hibernate-configuration>
The class of domain
package br.com.drogaria.domain;
import javax.annotation.Generated;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "tbl_fabricantes")
public class Fabricante {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "fab_codigo")
private Long codigo;
@Column(name = "fab_descricao", length = 50, nullable = false)
private String descricao;
public Long getCodigo() {
return codigo;
}
public void setCodigo(Long codigo) {
this.codigo = codigo;
}
public String getDescricao() {
return descricao;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
}
log of the execution shown in the console. Hibernate does not generate the table.
ago 22, 2017 11:56:46 am org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {5.2.10.Final} Aug 22, 2017 11:56:46 AM org.hibernate.cfg.Environment INFO: HHH000206: Hibernate.properties not found created configuration! Hibernate.cfg.xml registered! aug 22, 2017 11:56:47 AM org.hibernate.Annotations.common.Reflection.java.Javareflectionmanager INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final} aug 22, 2017 11:56:47 AM org.hibernate.engine.jdbc.Connections.internal.Drivermanagerconnectionproviderimpl configure WARN: HHH10001002: Using Hibernate built-in Connection pool (not for Production use!) ago 22, 2017 11:56:47 AM org.hibernate.engine.jdbc.Connections.internal.Drivermanagerconnectionproviderimpl buildCreator INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/db_drogaria] aug 22, 2017 11:56:47 AM org.hibernate.engine.jdbc.Connections.internal.Drivermanagerconnectionproviderimpl buildCreator INFO: HHH10001001: Connection properties: {user=root, password=***} aug 22, 2017 11:56:47 AM org.hibernate.engine.jdbc.Connections.internal.Drivermanagerconnectionproviderimpl buildCreator INFO: HHH10001003: Autocommit mode: false Aug 22, 2017 11:56:47 AM org.hibernate.engine.jdbc.Connections.internal.Pooledconnections INFO: HHH000115: Hibernate Connection pool size: 1 (min=1) ago 22, 2017 11:56:48 AM org.hibernate.dialect.Dialect INFO: HHH000400: Using dialect: org.hibernate.dialect.Mysql5innodbdialect Created Section! buildSessionFactory() method returns a sessionFactory from Hibernate.cfg.xml Aug 22, 2017 11:56:49 AM org.hibernate.engine.jdbc.Connections.internal.Drivermanagerconnectionproviderimpl STOP INFO: HHH10001008: Cleaning up Connection pool [jdbc:mysql://localhost:3306/db_drogaria] Geratable.java invokes the Hibernateutil.getSessionFactory() method to obtain a sessionFactory()
Geratable.java invokes the Hibernateutil.getSessionFactory() method to obtain a sessionFactory() Section is terminated by method Hibernateutil.getSessionFactory(). close();
Corrected typing error <!-Creates tables -> <Property name="hbm2dd1.auto">update</Property> "hbm2dd1.auto" to "hbm2ddl.auto"
– Salmir Ferreira de Araujo
Solved! To generate the table, I had to use "dialect = org.hibernate.dialect.Mysqlinnodbdialect"
– Salmir Ferreira de Araujo