最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

hibernate - javax.persistence.PersistenceException: No Persistence provider for EntityManager named exampleHibernateJPA` in Java

programmeradmin0浏览0评论

I'm encountering an issue while running a Java application that utilizes JPA for database operations. The application is designed to reset the database state by deleting existing data and populating it with predefined entries. However, upon execution, I receive the following error:

INFO : com.force.samples.util.DataLoadUtil | Resetting database state
Feb 04, 2025 12:05:08 PM javax.persistence.spi.PersistenceProviderResolverHolder$DefaultPersistenceProviderResolver log
WARNING: javax.persistence.spi::No valid providers found.
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named exampleHibernateJPA
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
    at com.force.samples.util.DataLoadUtil.resetDatabaseState(DataLoadUtil.java:34)
    at com.force.samples.util.DataLoadUtil.main(DataLoadUtil.java:30)

Code Snippet:

package com.force.samples.util;

import java.util.GregorianCalendar;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import .slf4j.Logger;
import .slf4j.LoggerFactory;
import com.force.samples.entity.Author;
import com.force.samples.entity.Book;

public class DataLoadUtil {
    
    private static Logger log = null;
    
    public static void main(String[] args) {
        log = LoggerFactory.getLogger(DataLoadUtil.class);
        
        log.info("Resetting database state");
        resetDatabaseState();
    }

    private static void resetDatabaseState() {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("exampleHibernateJPA");
        EntityManager em = emf.createEntityManager();
        
        log.info("Creating and persisting entity...");
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        
        deleteAllExistingData(em);
        populateCannedData(em);
        
        txmit();
    }

    private static void populateCannedData(EntityManager em) {
        Author author = new Author();
        author.setFirstName("JRR");
        author.setLastName("Tolkien");

        Book hobbit = new Book();
        hobbit.setAuthor(author);
        hobbit.setTitle("The Hobbit");
        hobbit.setPublicationDate(new GregorianCalendar(1937, 11, 1).getTime());
        em.persist(hobbit);
        
        Book fotr = new Book();
        fotr.setAuthor(author);
        fotr.setTitle("Fellowship of the Ring");
        fotr.setPublicationDate(new GregorianCalendar(1954, 6, 24).getTime());
        em.persist(fotr);
        
        Book twoTowers = new Book();
        twoTowers.setAuthor(author);
        twoTowers.setTitle("The Two Towers");
        twoTowers.setPublicationDate(new GregorianCalendar(1954, 10, 11).getTime());
        em.persist(twoTowers);
        
        Book rotk = new Book();
        rotk.setAuthor(author);
        rotk.setTitle("Return of the King");
        rotk.setPublicationDate(new GregorianCalendar(1955, 9, 20).getTime());
        em.persist(rotk);
        
        Author rowling = new Author();
        rowling.setFirstName("J.K");
        rowling.setLastName("Rowling");
        
        Book hpps = new Book();
        hpps.setAuthor(rowling);
        hpps.setTitle("Harry Potter and the Philosopher's Stone");
        hpps.setPublicationDate(new GregorianCalendar(1997, 5, 30).getTime());
        em.persist(hpps);
        
        Book hpcs = new Book();
        hpcs.setAuthor(rowling);
        hpcs.setTitle("Harry Potter and the Chamber of Secrets");
        hpcs.setPublicationDate(new GregorianCalendar(1998, 6, 2).getTime());
        em.persist(hpcs);
        
        Book hppa = new Book();
        hppa.setAuthor(rowling);
        hppa.setTitle("Harry Potter and the Prisoner of Azkaban");
        hppa.setPublicationDate(new GregorianCalendar(1999, 6, 8).getTime());
        em.persist(hppa);
    }

    private static void deleteAllExistingData(EntityManager em) {
        log.info("Deleting existing books");
        Query bookQuery = em.createQuery("delete from Book");
        bookQuery.executeUpdate();

        Query authorQuery = em.createQuery("delete from Author");
        authorQuery.executeUpdate();

        log.info("Deleting existing authors");
    }
}

Details:

  • Persistence Unit Name: exampleHibernateJPA
  • JPA Implementation: Hibernate
  • Dependencies:
    • hibernate-entitymanager
    • hibernate-core
    • javax.persistence-api
    • slf4j-api
    • slf4j-log4j12
    • log4j

Troubleshooting Steps Taken:

  1. Checked persistence.xml: ensured that the persistence.xml file is located in the META-INF directory and contains the correct persistence unit name.
  2. Verified Dependencies: confirmed that the required JPA and Hibernate dependencies are included in the project's build path.
  3. ClassPath Inspection: verified that the META-INF/persistence.xml is included in the classpath during runtime.

Request for Assistance:

I'm seeking guidance on resolving the javax.persistence.PersistenceException: No Persistence provider for EntityManager named exampleHibernateJPA error. There additional configurations or steps I might have overlooked to ensure that the persistence provider is correctly recognized? Any insights or suggestions would be greatly appreciated.

Additional Context:

The application is executed using the following command:

mvn -e exec:java -Dexec.mainClass=com.force.samples.util.DataLoadUtil

thank you in advance for your assistance.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论