Unable to build Entity manager Factory ( Hibernate JPA )

Asked

Viewed 601 times

1

I am trying to make a connection with Hibernate and JPA, but when trying to create the connection or persist some data it executes the following error:

Dec 04, 2018 2:02:45 pm org.hibernate.ejb.Hibernatepersistence logDeprecation WARN: HHH015016: Encountered a deprecated javax.persistence.spi.Persistenceprovider [org.hibernate.ejb.Hibernatepersistence]; use [org.hibernate.jpa.Hibernatepersistenceprovider] Instead. Dec 04, 2018 2:02:45 PM org.hibernate.ejb.Hibernatepersistence logDeprecation WARN: HHH015016: Encountered a deprecated javax.persistence.spi.Persistenceprovider [org.hibernate.ejb.Hibernatepersistence]; use [org.hibernate.jpa.Hibernatepersistenceprovider] Instead. Dec 04, 2018 2:02:45 PM org.hibernate.ejb.Hibernatepersistence logDeprecation WARN: HHH015016: Encountered a deprecated javax.persistence.spi.Persistenceprovider [org.hibernate.ejb.Hibernatepersistence]; use [org.hibernate.jpa.Hibernatepersistenceprovider] Instead. Dec 04, 2018 2:02:45 PM org.hibernate.jpa.internal.util.Loghelper logPersistenceUnitInformation INFO: HHH000204: Processing Persistenceunitinfo [ name: context ...] Dec 04, 2018 2:02:45 PM org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {4.3.0.Final} Dec 04, 2018 2:02:45 PM org.hibernate.cfg.Environment INFO: HHH000206: Hibernate.properties not found Dec 04, 2018 2:02:45 pm org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode Provider name : javassist Dec 04, 2018 2:02:45 PM org.hibernate.Annotations.common.Reflection.java.Javareflectionmanager INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final} Dec 04, 2018 2:02:45 pm org.hibernate.engine.jdbc.Connections.internal.Drivermanagerconnectionproviderimpl configure INFO: HHH000402: Using Hibernate built-in Connection pool (not for Production use!) Unable to build Entity manager Factory

My pom.xml

<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>gpnet.com.br.api</groupId>
    <artifactId>Api</artifactId>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java-version>1.8</java-version>
        <spring-version>5.0.4.RELEASE</spring-version>
        <jpa-version>2.0.4.RELEASE</jpa-version>
        <hibernate-version>4.3.0.Final</hibernate-version>
        <mysql-version>6.0.4</mysql-version>
        <hibernate-jpa-version>1.0.0.Final</hibernate-jpa-version>
    </properties>

    <dependencies>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring-version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring-version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>${jpa-version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate-version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate-version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-version}</version>
            <scope>compile</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.1-api -->
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>${hibernate-jpa-version}</version>
        </dependency>


    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven</groupId>
                <artifactId>maven-plugin-api</artifactId>
                <version>3.3.3</version>
                <configuration>
                    <source>${java-version}</source>
                    <target>${java-version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

My persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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">

    <persistence-unit name="context" transaction-type="RESOURCE_LOCAL">

        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <!-- Configurações da string de conexão -->
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jpa"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value="root"/>
            <!-- Linguagem que o Hibernate irá usar para se comunicar com o banco -->
            <property name="hibernate.dialect" value="hibernate.dialect.MYSQL5Dialect"/>
            <!-- Mostrar as queries no console -->
            <property name="hibernate.show_sql" value="true"/>
            <!-- Atualizar o banco de acordo com alguma alteração -->

        </properties>

    </persistence-unit>
</persistence>

My Main class

package gpnet.com.br.main;

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

import gpnet.com.br.models.Usuario;
import gpnet.com.br.util.JPAUtil;

public class Main {

    public static void main(String[] args) {

        Usuario user = new Usuario();
        user.setNome("jose");
        user.setEmail("jose.silva");
        user.setSenha("123456");

        try {
            EntityManager em;
            EntityManagerFactory fact = Persistence.createEntityManagerFactory("context"); // Here it crashes
            em = fact.createEntityManager();
            em.getTransaction().begin();
            em.persist(user);
            em.getTransaction().commit();

        }catch(Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

2 answers

1


When analyzing better they lacked some dependencies in the pom.xml:

 <!-- API JPA -->
<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>javax.persistence</artifactId>
    <version>2.1.0</version>
</dependency>

<!-- JXB -->
<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
</dependency>

As well as in the persistence.xml the hibernate.dialect has to be in accordance with the version of the bank you are using.

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL57Dialect"/>

In my case the version 5.7 of MYSQL

0

Apparently, the JPA implementation you are using this depreciated.

In your persistence.xml exchange the Provider tag for this: <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

Note also that you need to indicate to JPA which classes are entities to be persisted by adding this tag after declaring Provider:

<class>gpnet.com.br.models.Usuario</class>
  • I made the changes but the error remains the same Unable to build entity manager factory

  • I think there may be another error in persistence.xml. You have entered the name of <persistence unit/> as "context", but in your database url, the name is "jpa". Try changing this url to: jdbc:mysql://localhost:3306/context

  • I switched, created the bank as context, but nothing. Keeps giving this error Unable to build entity manager factory .

  • Maybe some dependency on Maven is outdated, so what I know Hibernate-core is the new distribution and has all the components that Hibernate needs. Try to get rid of that dependency: hibernate-entitymanager from Maven and update the most updated Hibernate-core from this link https://mvnrepository.com/artifact/org.hibernate/hibernate-core/5.3.Final

  • I removed all the dependencies and left only the hibernate and the connector mysql all with the latest version. The error has now changed: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

  • It’s hard to say for sure what the problem is. I have an application with Hibernate and it’s running well with just these two dependencies: mysql-connector this link https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.13; and hibernate-core this link https://mvnrepository.com/artifact/org.hibernate/hibernate-core/5.3.7.Final. I can give you the basis of my code to try to correct yours

  • would appreciate as already updated all dependencies and the error persists.

  • 1

    https://github.com/andre237/Hibernate-JPA-Test. I use Intellij but have a look at src who has everything he needs.

  • vlw guy helped a lot, but I ended up discovering that was missing some dependencies and the hibernate.dialect was not in agreement with the selected bank.

Show 4 more comments

Browser other questions tagged

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