没有EntityManager的持久性提供者命名为...错误 [英] No Persistence provider for EntityManager named... error

查看:100
本文介绍了没有EntityManager的持久性提供者命名为...错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的持久xml文件就是这样

 <?xml version =1.0encoding =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>
<属性>
< property name =hibernate.hbm2ddl.autovalue =create/>
< property name =hibernate.show_sqlvalue =true/>
< property name =hibernate.dialectvalue =org.hibernate.dialect.MySQL5InnoDBDialect/>
< property name =hibernate.connection.driver_classvalue =com.mysql.jdbc.Driver/>
< property name =hibernate.connection.usernamevalue =root/>
< property name =hibernate.connection.passwordvalue =2643/>
< property name =hibernate.connection.urlvalue =jdbc:mysql:// localhost:3306 / BusinessDb/>

< / properties>
< / persistence-unit>
< /余辉>

然后我用我的代码调用它:

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





我得到以下错误信息:

 线程main中的异常javax.persistence.PersistenceException:否在EntityManager中的名称为hibernateEbru 
的持久性提供程序。 persistence.Persistence.createEntityManagerFactory(Persistence.java:56)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
at com.hibernate.test.main(test.java:8)


解决方案

您需要将persistence.xml文件移至一个合适的位置



来自JPA规范:

 一个persistence.xml文件定义了一个持久性单元。 persistence.xml文件是位于持久性单元根目录的META-INF目录中的

持久单元的根是这里的关键。



如果您是非Java EE应用程序

 其META-INF目录包含persistence.xml 
文件的文件或目录称为持久性单元的根。

如果您在Java EE应用程序中,以下内容有效

 在Java EE环境中,持久性单元的根必须是下列之一:
•一个EJB-JAR文件
• WAR文件的WEB-INF / classes目录[80]
•WAR文件的WEB-INF / lib目录中的jar文件
•EAR库目录中的jar文件
•应用程序客户端jar文件


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)

解决方案

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

这篇关于没有EntityManager的持久性提供者命名为...错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆