Migration of Hibernate 4.3.x to 5.3.1

Asked

Viewed 1,412 times

0

I’m taking a course in Sales System, the teacher uses the version 4.3.11 of Hibernate(what works perfectly).

But I would like to migrar for the current version(5.3.1), but with the configuration of the past version, the one given in the course, it is not possible to make the persistence in the database.

This is the file code Hibernate.cfg.xml (version 4.3.11, that’s working):

<?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 com o Banco -->
        <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/vendas?useTimezone=true&amp;serverTimezone=UTC</property>
        <property name="connection.username">root</property>
        <property name="connection.password">xxxxx</property>
        <property name="hibernate.connection.autocommit">false</property>

        <!-- Config da conexao JDBC Mysql -->
        <property name="connection.pool_size">1</property>

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

        <!-- Gerenciamento da sessão mysql -->
        <property name="current_session_context_class">thread</property>

        <!-- Desabilita o cache -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Exibe os comando SQL -->
        <property name="show_sql">true</property>

        <!-- Criação das tabelas -->
        <property name="hbm2ddl.auto">update</property>

        <mapping class="br.com.livraria.domain.Fornecedores" />
        <mapping class="br.com.livraria.domain.Funcionarios" />

        <!-- <mapping class="br.com.livraria.domain.Produtos" /> 
        <mapping class="br.com.livraria.domain.Vendas" /> 
        <mapping class="br.com.livraria.domain.Item" /> -->

    </session-factory>

</hibernate-configuration>

Hibernateutil class.java:

package br.com.livraria.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 final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
    try {
        // Cria uma conexão a partir do hibernate.cfg.xml

        Configuration configuration = new Configuration();
        configuration.configure();

        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).build();


        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

        return sessionFactory;

        //return new Configuration().configure().buildSessionFactory(
          //  new StandardServiceRegistryBuilder().build() );
    }
    catch (Throwable ex) {
        // Mensagem de erro ao conectar
        System.err.println("Erro na conexão." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}
public static SessionFactory getSessionFactory() {
    return sessionFactory;
}}

Result of console when I change the Hibernate version to 5.3.1.FINAL:

jun 02, 2018 7:06:38 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.3.1.Final}

jun 02, 2018 7:06:38 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found

jun 02, 2018 7:06:39 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.3.Final}

jun 02, 2018 7:06:40 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)

jun 02, 2018 7:06:40 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.cj.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/vendas?useTimezone=true&serverTimezone=UTC]

jun 02, 2018 7:06:40 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}

jun 02, 2018 7:06:40 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false

jun 02, 2018 7:06:40 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)

Sat Jun 02 19:06:40 BRT 2018 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.

jun 02, 2018 7:06:40 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect

jun 02, 2018 7:06:40 PM org.hibernate.resource.beans.spi.ManagedBeanRegistryInitiator resoveBeanContainer
INFO: HHH10005002: No explicit CDI BeanManager reference was passed to Hibernate, but CDI is available on the Hibernate ClassLoader.

jun 02, 2018 7:06:41 PM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@3f6db3fb] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.

jun 02, 2018 7:06:41 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool[jdbc:mysql://localhost:3306/vendas?useTimezone=true&serverTimezone=UTC]

I believe the problem is in the item jun 02, 2018 7:06:41 PM but I couldn’t identify a solution. I’m using the Eclipse, MAVEN, JSF and JUnit.

  • 1

    I had trouble with migration, too. In case I didn’t use the file "HIBERNATE.CFG.XML" I think the error is in PERSISTENCE.XML, from a look at a post I did recently, I think it should help https://answall.com/questions/247447/no-persistence-provider-for-entitymanager-para-o-hibernate-5-x

2 answers

2

Read on: http://www.thejavageek.com/jpa-tutorials/

But a very simple example would be:

Filing cabinet persistence.xml

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
         http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
         version="2.1">

<persistence-unit name="PU" transaction-type="RESOURCE_LOCAL">
    <description> Hibernate JPA Configuration Example</description>
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" /> <!-- DB Driver ALTERE-->
        <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://172.17.0.2/academy" /> <!-- BD ALTERE -->
        <property name="javax.persistence.jdbc.user" value="postgres" /> <!-- DB User -->
        <property name="javax.persistence.jdbc.password" value="123456" /> <!-- DB Password -->

        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgresPlusDialect"/> <!-- Altere pro mysql -->
        <property name="hibernate.hbm2ddl.auto" value="update" /> <!-- create / create-drop / update -->

        <property name="hibernate.show_sql" value="true" /> <!-- Show SQL in console -->
        <property name="hibernate.format_sql" value="true" /> <!-- Show SQL formatted -->
    </properties>
</persistence-unit>

Filing cabinet Pessoa.java

@Entity 
public class Pessoa {
    @Id @GeneratedValue
    private Long id;

    private String nome;

    public String getNome() {
        return nome;
    }

    public void setNome(String name) {
        this.nome = name;
    }

    public Long getId() {
        return id;
    }

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

    public Pessoa(String nome, Long id) {
        this.nome = nome;
        this.id = id;
    }

    public Pessoa(String nome){
        this(nome,null);
    }

    public Pessoa(){}

}

Main file.java

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class Main {
    private static EntityManager em;

    public static void main(String[] args) {
     EntityManagerFactory emf = Persistence
            .createEntityManagerFactory("PU");
     em = emf.createEntityManager();
     em.getTransaction().begin();
     em.persist(new Pessoa("NOMEQUALQUER"));
     em.getTransaction().commit();
   }
}
  • Thanks for the answer. But it didn’t solve the problem, I’m using pure Hibernate. I do not have persistence.xml and when I include it (keeping the two Xml and Persistence.xml files, the latter is not read) and if I leave only persistence.xml Hibernate complains about the lack of cfg.xml.

1

From I do not know which version the Hibernate went increasingly towards the JPA specification and they strongly recommend this form of work from now on.

Some things like Criteria, Hibernateentitymanager, for example, are already deprecated and soon will disappear from the framework or no longer have maintenance.

I don’t know the size of your project, but if you want to go to 5.3.1 it is interesting to study JPA and migrate it. The change is not simple, but recommended to work with newer versions of Hibernate. I did it here and gave a little work because of the size of the projects, but in the end everything worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.