没有名为 MyPersistenceUnit 的 EntityManager 持久性提供程序

问题描述 投票:0回答:1

我一直在寻找这个问题的解决方案,但已经放弃了。 我几乎尝试了所有可能的方法,但仍然没有用。我知道这可能是重复的,但相信我,我几乎尝试了所有可能的方法。

在我使用 Maven 的 servlet 项目中,我一直面临着这个

No Persistence provider for EntityManager named MyPersistenceUnit
异常。不幸的是,尽管我已经研究了几乎所有可能的解决方案,但没有任何帮助。

这是我的

persistence.xml
文件。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns="https://jakarta.ee/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_1.xsd"
             version="3.1">
    <persistence-unit name="MyPersistenceUnit">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <!-- Specify entity classes -->
        <class>com.example.Student</class>
        <class>com.example.Accounts</class>


        <properties>
            <property name="jakarta.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/Network2HW"/>
            <property name="jakarta.persistence.jdbc.user" value="admin"/>
            <property name="jakarta.persistence.jdbc.password" value="123456"/>
            <property name="jakarta.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>

        </properties>
    </persistence-unit>
</persistence>

这是我的 DAO 类,其中存在问题。

package com.example.networksiihw;

import com.example.Accounts;

import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.EntityTransaction;
import jakarta.persistence.Persistence;
import java.util.List;

public class AccountsDAO {

    private final EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("MyPersistenceUnit");

    public void addAccounts(Accounts accounts) {
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        EntityTransaction transaction = entityManager.getTransaction();

        try {
            transaction.begin();
            entityManager.persist(accounts);
            transaction.commit();
        } catch (Exception e) {
            if (transaction != null && transaction.isActive()) {
                transaction.rollback();
            }
            e.printStackTrace();
        } finally {
            entityManager.close();
        }
    }

    public Accounts getAccountsById(Long id) {
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        Accounts accounts = entityManager.find(Accounts.class, id);
        entityManager.close();
        return accounts;
    }

    public List<Accounts> getAllAccounts() {
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        List<Accounts> accountsList = entityManager.createQuery("SELECT a FROM Accounts a", Accounts.class).getResultList();
        entityManager.close();
        return accountsList;
    }

    // Add additional methods for update and delete if needed
}

这是我的

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>NetworksIIHw</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>NetworksIIHw</name>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>11</maven.compiler.target>
        <maven.compiler.source>11</maven.compiler.source>
        <junit.version>5.9.2</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>jakarta.platform</groupId>
            <artifactId>jakarta.jakartaee-api</artifactId>
            <version>9.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.github.cdimascio</groupId>
            <artifactId>dotenv-java</artifactId>
            <version>3.0.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-core -->
        <dependency>
            <groupId>org.hibernate.orm</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>6.3.0.CR1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/jakarta.persistence/jakarta.persistence-api -->
        <dependency>
            <groupId>jakarta.persistence</groupId>
            <artifactId>jakarta.persistence-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.15.2</version>
        </dependency>



        <!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <version>8.0.33</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.2</version>
            </plugin>
        </plugins>
    </build>
</project>

老实说,我不知道我错过了什么。如果我需要提供任何额外的东西,请询问,我将提供任何有用的额外信息

java hibernate jpa servlets
1个回答
0
投票

我不知道坚持

jakarta
和其他依赖项对您有多重要,但我在这里使用
hibernate-core
创建了一个示例:stackoverflow-persistence-xml

当我编写示例时,我注意到您的

persistence.xml
中的提供者是

<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

根据您使用的具体 Hibernate 版本,更改为 就足够了

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

如果它不起作用,您可以使用 GitHub 存储库中的示例。

© www.soinside.com 2019 - 2024. All rights reserved.