我正在尝试按照Hibernate EntityManager documentation中的信息设置一个简单的jpa 2.0项目。我已经在这几个小时了,但不管我做什么,当我尝试创建一个EntityManagerFactory时,我总是得到这个例外:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named manager1
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
at se.mycomp.UserTest.main(UserTest.java:9)
我发现了很多关于这个例外的类似问题,但没有解决方案我可以开始工作。我在这里做错了什么?
目录结构
.
├── pom.xml
└── src
├── main
│ ├── java
│ │ └── se
│ │ └── mycomp
│ │ ├── UserTest.java
│ │ └── domain
│ │ └── User.java
│ └── resources
│ ├── META-INF
│ │ └── persistence.xml
│ └── log4j.properties
└── test
└── java
我的 persistence.xml
<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_2_0.xsd"
version="2.0">
<persistence-unit name="manager1" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>se.mycomp.domain.User</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/test"/>
<property name="javax.persistence.jdbc.user" value="test"/>
<property name="javax.persistence.jdbc.password" value="1234"/>
</properties>
</persistence-unit>
</persistence>
我的 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>se.lil.tryjpa</groupId>
<artifactId>try-jpa</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate-core.version>3.6.4.Final</hibernate-core.version>
<mysql-connector-java.version>5.1.16</mysql-connector-java.version>
<slf4j.version>1.6.1</slf4j.version>
<log4j.version>1.6.1</log4j.version>
</properties>
<dependencies>
<!-- HIBERNATE DEPENDENCIES -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate-core.version}</version>
</dependency>
<!-- MYSQL DEPENDENCIES -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
</dependency>
<!-- Logging Dependencies -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<optimize>true</optimize>
<debug>true</debug>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
<downloadSources>true</downloadSources>
</configuration>
</plugin>
</plugins>
</build>
的 UserTest.java 的
public class UserTest {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("manager1");
EntityManager em = emf.createEntityManager();
}
}
答案 0 :(得分:45)
也许您错过了您的pom.xml依赖项中的Provider类或其依赖项之一?
您提供给hibernate文档的链接表示您还应添加
<project ...>
...
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate-core-version}</version>
</dependency>
</dependencies>
</project>
到你的pom.xml
答案 1 :(得分:1)
persistence.xml意味着存在于META-INF目录中,而META-INF意味着存在于应用程序的类路径中,即src文件夹。
根据你的文件夹结构,它存在于资源文件夹中,尝试将其移动到类路径,它会影响工作。
答案 2 :(得分:0)
我使用下面的依赖项和以下提供程序解决了这个问题:
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1200-jdbc41</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>LATEST</version>
</dependency>
最新版本不适用于您,您可以使用 1.0.1.Final为hibernate-jpa-2.0-api 和5.2.5.Final for hibernate-entitymanager
另外,在persistence.xml中,不要忘记hbm2dll.auto属性(这是我花了一些时间搞清楚的问题)
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.show_sql" value="true"/>
然后进行maven clean install