Java JPA + Hibernate = EntityManager 没有持久化提供程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26468910/
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
JPA + Hibernate = No Persistence provider for EntityManager
提问by Andrea
I'm trying to setup JPA for my Maven project, using Hibernate as provider.
我正在尝试为我的 Maven 项目设置 JPA,使用 Hibernate 作为提供者。
Structure of the project
项目结构
├── META-INF
│ └── persistence.xml
├── src
| ├── main
| | └── java
| | ├── model
| | | └── Instance.java
| | └── App.java
| └── test
| └── java
| └── model
| └── AppTest.java
└── pom.xml
Content of persistence.xml
persistence.xml 的内容
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" version="2.1">
<persistence-unit name="testjpa" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>it.vitrociset.model.Instance</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/aquasystem"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.connection.username" value="username"/>
<property name="hibernate.connection.password" value="password"/>
</properties>
</persistence-unit>
</persistence>
Content of Instance.java
Instance.java 的内容
package model;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Instance {
@Id
private String id;
private String path;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
Content of App.java
App.java的内容
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
public class App {
public static void main( String[] args ) {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("testjpa");
EntityManager em = entityManagerFactory.createEntityManager();
EntityTransaction userTransaction = em.getTransaction();
userTransaction.begin();
Instance instance = new Instance();
instance.setId("id");
instance.setPath("path");
em.persist(instance);
userTransaction.commit();
em.close();
entityManagerFactory.close();
}
}
Content of pom.xml
pom.xml 的内容
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId></groupId>
<artifactId></artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.6.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.6.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
</dependencies>
</project>
I got the following exception:
我得到以下异常:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named testjpa
but I have no idea why. What is wrong in my code? I wasn't able to find the solution.
但我不知道为什么。我的代码有什么问题?我无法找到解决方案。
采纳答案by Tomasz W
Persistence.xml is not in resources directory and therefore not in classpath. Move META-INF directory to src/main/resources.
Persistence.xml 不在资源目录中,因此不在类路径中。将 META-INF 目录移动到 src/main/resources。
回答by Harrhy Saladagu
Add hibernate-entitymanager.jar to the classpath to resolve this issue.
将 hibernate-entitymanager.jar 添加到类路径以解决此问题。
回答by geckos
I had this problem with hibernate 5. Switching to 4.3.11.Final fix it. The org.hibernate.ejb.HibernatePersistence wasn't in classpath.
我在 hibernate 5 中遇到了这个问题。切换到 4.3.11.Final 修复它。org.hibernate.ejb.HibernatePersistence 不在类路径中。