GeraTabela class with 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();");
}
}
A 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 hibernate.cfg.xml configuration file
<?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 domain class
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;
}
}
running log shown on the console. Hibernate does not generate the table.
Aug 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 INFO: HHH000206: org.hibernate.cfg.Environment: hibernate.properties not found configuration created! hibernate.cfg.xml registered! Aug 22, 2017 11:56:47 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager INFO: HCANN000001: Hibernate Commons Annotations 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!) Aug 22, 2017 11:56:47 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl INFO: buildCreator: 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 INFO BuildCreator: 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] GeraTabela.java invokes the hibernateUtil.getSessionFactory () method to get a sessionFactory ()
GeraTabela.java invokes the HibernateUtil.getSessionFactory () method to get a sessionFactory () section is terminated by method HibernateUtil.getSessionFactory (). Close ();