No Persistence provider for EntityManager named… error [duplicate]

心不动则不痛 提交于 2019-12-11 03:34:47

问题


My persistence xml file is like that

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    version="1.0">
    <persistence-unit name="hibernateEbru">     
        <provider>org.hibernate.ejb.HibernatePersistence</provider>     
        <class>com.hibernate.business_card</class> 
        <properties>            
            <property name="hibernate.hbm2ddl.auto" value="create" />           
            <property name="hibernate.show_sql" value="true" />         
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />         
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />         
            <property name="hibernate.connection.username" value="root" />          
            <property name="hibernate.connection.password" value="2643" />          
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/BusinessDb" /> 

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

Then I have my code calling it with this:

public class test {
    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("hibernateEbru");

        EntityManager em = emf.createEntityManager();

        em.getTransaction().begin();

        business_card bc = new business_card();
        bc.setName("Ebru");

        em.persist(bc);

        em.getTransaction().commit();

        em.close();

        emf.close();

    }
}

I got the following error message:

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named hibernateEbru
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:56)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
    at com.hibernate.test.main(test.java:8)

回答1:


You will need to move the persistence.xml file to an appropriate location

From JPA spec:

A persistence.xml file defines a persistence unit. The persistence.xml file is located in the META-INF directory of the root of the persistence unit.

The root of the persistence unit is the key here.

If you are a non-Java EE app

The jar file or directory whose META-INF directory contains the persistence.xml file is termed the root of the persistence unit.

If you are in a Java EE app, the following are valid

In Java EE environments, the root of a persistence unit must be one of the following:

  • an EJB-JAR file
  • the WEB-INF/classes directory of a WAR file[80]
  • a jar file in the WEB-INF/lib directory of a WAR file
  • a jar file in the EAR library directory
  • an application client jar file


来源:https://stackoverflow.com/questions/18829834/no-persistence-provider-for-entitymanager-named-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!