Tables are not created automatically by Hibernate in MySQL

2

Hibernate can not create the table in the database already created (drugstore). Follow the code below.

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 de Conexão com o Banco de Dados -->
        <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://127.0.0.1:3306/drogaria_teste?useTimezone=true&amp;serverTimezone=UTC</property>
        <property name="connection.username">root</property>
        <property name="connection.password">q1w2e3r4</property>

        <!-- Pool de Conexões -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>

        <!-- Gerenciamento do Contexto das Sessões -->
        <property name="current_session_context_class">thread</property>

        <!-- Cache de Segundo Nível -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Mostra as SQLs Geradas -->
        <property name="show_sql">true</property>

        <!-- Cria as tabelas do banco de dados -->
        <property name="hbm2ddl.auto">create</property>

        <!-- Mapeamento das Entidades -->
        <mapping class="br.alois.drogaria.domain.Estado" />

    </session-factory>

</hibernate-configuration>

Now the State.java class

package br.alois.drogaria.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

@SuppressWarnings("serial")
@Entity
@Table(name = "estado")
public class Estado extends GenericDomain {

    @Column(length = 2, nullable = false)
    private String sigla;

    @Column(length = 50, nullable = false)
    private String nome;

    // getters e setters

}

The POM file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>br.alois.drogaria</groupId>
<artifactId>Drogaria</artifactId>
<version>0.5 teste</version>
<packaging>war</packaging>

<!-- Codificação dos Caracteres -->
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<!-- Parâmetros de Execução -->
<build>
    <!-- Nome do projeto empacotado -->
    <finalName>Drogaria</finalName>

    <!-- Plugins -->
    <plugins>
        <!-- Compilador -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

<!-- Dependências -->
<dependencies>
    <!-- Hibernate Core -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.2.2.Final</version>
    </dependency>

    <!-- Conector MySQL -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>6.0.3</version>
    </dependency>

    <!-- JUnit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>

</dependencies>

HibernateUtil:

package br.alois.drogaria.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {

    private static SessionFactory fabricaDeSessoes = criarFabricaDeSessoes();

    public static SessionFactory getFabricaDeSessoes() {
        return fabricaDeSessoes;
    }

    private static SessionFactory criarFabricaDeSessoes() {
        try {
            Configuration configuracao = new Configuration().configure();

            ServiceRegistry registro = new StandardServiceRegistryBuilder().applySettings(configuracao.getProperties())
                    .build();

            SessionFactory fabrica = configuracao.buildSessionFactory(registro);

            return fabrica;
        } catch (Throwable ex) {
            System.err.println("A fábrica de sessões não pode ser criada." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

}

Here's the class to test the creation of the table:

package br.alois.drogaria.util;

import org.hibernate.Session;
import org.junit.Test;

public class HibernateUtilTest {
    @Test
    public void conectar(){
        Session sessao = HibernateUtil.getFabricaDeSessoes().openSession();
        sessao.close();
        HibernateUtil.getFabricaDeSessoes().close();
    }
}

Below the Tomcat 9 console log:

ago 16, 2016 8:54:59 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.2.Final}
ago 16, 2016 8:54:59 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
ago 16, 2016 8:54:59 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
ago 16, 2016 8:55:00 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
ago 16, 2016 8:55:00 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
ago 16, 2016 8:55:00 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.cj.jdbc.Driver] at URL [jdbc:mysql://127.0.0.1:3306/drogaria_teste?useTimezone=true&serverTimezone=UTC]
ago 16, 2016 8:55:00 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
ago 16, 2016 8:55:00 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
ago 16, 2016 8:55:00 AM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Tue Aug 16 08:55:00 GMT-03:00 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
ago 16, 2016 8:55:01 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
ago 16, 2016 8:55:01 AM org.hibernate.tool.schema.internal.SchemaCreatorImpl applyImportSources
INFO: HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@13187b0'
ago 16, 2016 8:55:02 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://127.0.0.1:3306/drogaria_teste?useTimezone=true&serverTimezone=UTC]

What is missing or what is wrong?

    
asked by anonymous 16.08.2016 / 14:11

2 answers

1

I think there in persistence.xml where it has:

<property name="hbm2ddl.auto">create</property>

switch by:

 <property name="hibernate.hbm2ddl.auto">create</property>
    
18.08.2016 / 15:02
1

Thank you for helping Lindemberg Barbosa.

I was able to resolve the line by removing the row argument from the line:

SessionFactory fabrica = configuracao.buildSessionFactory(registro);

So he searched for Hibernate.cfg.xml . In this he showed me a concept I did not know was Hibernate_sequence . Hibernate created a table that guides to another table, in case of inheritance, which was the next id. Resolved.

    
20.08.2016 / 03:43