Hibernate error: No Persistence provider for EntityManager named HibernateEjemplo1

165 Views Asked by At

I'm a programming student and this is my first contact with Hibernate.

When trying to make a program with netbeans 16 and JDK 19, using Hibernate to connect to a SQL database I get the following error.


    Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named HibernateEjemplo1
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
        at test.HolaMundoHibernate.main(HolaMundoHibernate.java:12)
    Command execution failed.

Following an internet tutorial I have done the following:

  1. I have created the database called test and the table called persona with SQL.

  2. I have created a java Maven project, called HibernateExample1 with Netbeans 16 and JDK 19.

  3. I have modified the pom.xml file to add dependencies. I have only added the ones highlighted in blue, the rest have been added by themselves by doing clean & build in the project.


    <?xml version="1.0" encoding="UTF-8"?>
    <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>mx.com.gm</groupId>
        <artifactId>HibernateEjemplo1</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>jar</packaging>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>19</maven.compiler.source>
            <maven.compiler.target>19</maven.compiler.target>
            <exec.mainClass>HibernateEjemplo1</exec.mainClass>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.hibernate.orm</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>6.1.6.Final</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.28</version>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-api</artifactId>
                <version>2.13.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>2.13.0</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate.javax.persistence</groupId>
                <artifactId>hibernate-jpa-2.1-api</artifactId>
                <version>1.0.0.Final</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>org.eclipse.persistence.core</artifactId>
                <version>2.7.10</version>
            </dependency>
            <dependency>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>org.eclipse.persistence.asm</artifactId>
                <version>9.2.0</version>
            </dependency>
            <dependency>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>org.eclipse.persistence.antlr</artifactId>
                <version>2.7.10</version>
            </dependency>
            <dependency>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>org.eclipse.persistence.jpa</artifactId>
                <version>2.7.10</version>
            </dependency>
            <dependency>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>org.eclipse.persistence.jpa.jpql</artifactId>
                <version>2.7.10</version>
            </dependency>
            <dependency>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>org.eclipse.persistence.moxy</artifactId>
                <version>2.7.10</version>
            </dependency>
            <dependency>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>jakarta.persistence</artifactId>
                <version>2.2.3</version>
            </dependency>
        </dependencies>
    </project>

  1. I have created the persistence.xml file

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.2" xmlns="http://java.sun.com/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_2_2.xsd">
      <persistence-unit name="HibernateEjemplo1" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>mx.com.gm.domain.Persona</class>
        <properties>
          <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mysql?zeroDateTimeBehavior=CONVERT_TO_NULL"/>
          <property name="javax.persistence.jdbc.user" value="root"/>
          <property name="javax.persistence.jdbc.password" value="password"/>
          <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
          <property name="hibernate.show_sql" value="true"/>
          <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
        </properties>
      </persistence-unit>
    </persistence>
    
  2. I have created the file log4j2.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="INFO">
        <Appenders>
            <Console name="Console" target="SYSTEM_OUT">
                <PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/>
            </Console>
        </Appenders>
        <Loggers>
            <Logger name="org.hibernate.SQL" level="debug" additivity="false">
                <AppenderRef ref="Console"/>
            </Logger>
            <Logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="trace" additivity="false">
                <AppenderRef ref="Console"/>
            </Logger>
            <Root level="info">
                <AppenderRef ref="Console"/>
            </Root>
        </Loggers>
    </Configuration>
    
  3. I have created the class Persona.java which has the same attributes as my SQL table.

    package mx.com.gm.domain;
    
    import java.io.Serializable;
    import javax.persistence.*;
    
    @Entity
    @Table(name = "persona")
    public class Persona implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        @Column(name = "id_persona")
        @Id
        private int idPersona;
    
        private String nombre;
    
        private String apellido;
    
        private String email;
    
        private String telefono;
    
        public Persona() {
        }
    
        public int getIdPersona() {
            return idPersona;
        }
    
        public void setIdPersona(int idPersona) {
            this.idPersona = idPersona;
        }
    
        public String getNombre() {
            return nombre;
        }
    
        public void setNombre(String nombre) {
            this.nombre = nombre;
        }
    
        public String getApellido() {
            return apellido;
        }
    
        public void setApellido(String apellido) {
            this.apellido = apellido;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public String getTelefono() {
            return telefono;
        }
    
        public void setTelefono(String telefono) {
            this.telefono = telefono;
        }
    
        @Override
        public String toString() {
            return "Empleados{" + "idPersona=" + idPersona + ", nombre=" + nombre + ", apellido=" + apellido + 
                    ", email=" + email + ", telefono=" + telefono + '}';
        }
    
    }
        ```
    
    
  4. And finally, I have created the class HolaMundoHibernate.java to run the program.

    package test;
    
    import java.util.List;
    import javax.persistence.*;
    import mx.com.gm.domain.Persona;
    
    
    public class HolaMundoHibernate {
        public static void main(String[] args) {
            String hql = "SELECT p FROM Persona p";
            EntityManagerFactory fabrica = Persistence.createEntityManagerFactory("HibernateEjemplo1");
            EntityManager entityManager = fabrica.createEntityManager();
    
            Query query = entityManager.createQuery(hql);
            List<Persona> personas = query.getResultList();
    
            for (Persona p : personas) {
                System.out.println("Persona: " + p);
            } 
    
        }
    }
    

When I run the program I get the following error: No Persistence provider for EntityManager named HibernateEjemplo1

The structure of my project is as follows.

Project structure

Any help is welcome. Thank you in advance.

0

There are 0 best solutions below