Java EntityManager 没有持久性提供程序命名...错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18829834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No Persistence provider for EntityManager named... error
提问by ebruszl
My persistence xml file is like that
我的持久化xml文件就是这样
<?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)
回答by cmd
You will need to move the persistence.xml file to an appropriate location
您需要将 persistence.xml 文件移动到适当的位置
From JPA spec:
从 JPA 规范:
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.
persistence.xml 文件定义了一个持久性单元。persistence.xml 文件位于持久性单元根目录的 META-INF 目录中。
The root of the persistence unit is the key here.
持久化单元的根是这里的关键。
If you are a non-Java EE app
如果您是非 Java EE 应用程序
The jar file or directory whose META-INF directory contains the persistence.xml file is termed the root of the persistence unit.
META-INF 目录包含persistence.xml 文件的jar 文件或目录称为持久性单元的根。
If you are in a Java EE app, the following are valid
如果您在 Java EE 应用程序中,以下内容是有效的
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
在 Java EE 环境中,持久性单元的根必须是以下之一:
- 一个 EJB-JAR 文件
- WAR 文件的 WEB-INF/classes 目录 [80]
- WAR文件的WEB-INF/lib目录下的jar文件
- EAR 库目录中的 jar 文件
- 应用程序客户端 jar 文件