netbeans 休眠错误:名为HibernateEjemplo1的EntityManager没有持久性提供程序

63lcw9qa  于 2023-01-05  发布在  其他
关注(0)|答案(1)|浏览(113)

我是一名编程专业的学生,这是我第一次接触Hibernate。
当尝试用netbeans 16和JDK 19制作程序时,使用Hibernate连接到SQL数据库,我得到以下错误。

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.

以下互联网教程,我做了以下:
1.我已经用SQL创建了名为test的数据库和名为persona的表。
1.我使用Netbeans 16和JDK 19创建了一个名为HibernateExample1的java Maven项目。
1.我已经修改了pom.xml文件来添加依赖项。我只添加了用蓝色突出显示的那些,其余的都是通过在项目中执行clean & build而自己添加的。

<?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.我已经创建了persistence.xml文件

<?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>

1.我已经创建了文件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>

1.我已经创建了类Persona.java,它与我的SQL表具有相同的属性。

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 + '}';
    }

}
    ```

1.最后,我创建了类HolaMundoHibernate.java来运行这个程序。

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);
        } 

    }
}

当我运行程序时,我得到以下错误:名为HibernateEjemplo1的实体管理器没有持久性提供程序
我的项目结构如下。
Project structure
任何帮助都是受欢迎的。先谢了。

pkwftd7m

pkwftd7m1#

“导入javax.persistence.*;“进口雅加达.持久性. *;在你的类HolaMundoHibernate和类Persona中。但你有这些问题,请帮我解决。

相关问题