EntityManager Hibernate 5 没有持久性提供程序
No Persistence provider for EntityManager Hibernate 5
我有以下项目结构,它是一个 multi-module maven 项目。我的问题出在持久性配置中(位于模型模块中)。它生成我的 war 而没有返回错误,但是在实例化 EntityManager 时,它会抛出一个 javax.persistence.PersistenceException: No Persistence provider for EntityManager named myPersistence
PARENT
|
|-DATABASE
| └ src/main/
| └ pom.xml
|
|-MODEL
| |- src/main
| | └ java/
| | └ .../JPAUtil.java
| | └ ressources
| | └ META-INF
| | └ persistence.xml
| └ pom.xml
|
|-WS
| └ src/main/
| └ pom.xml
|
└ pom.xml
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="myPersistence" >
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/mySchema" />
<property name="javax.persistence.jdbc.user" value="user" />
<property name="javax.persistence.jdbc.password" value="password" />
</properties>
</persistence-unit>
</persistence>
PARENT - 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>my.group.id</groupId>
<artifactId>parent</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<modules>
<module>modele</module>
<module>database</module>
<module>WS</module>
</modules>
<properties>
<!-- encoding-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--java version -->
<java-version>1.7</java-version>
<!-- plugin versions -->
<war-plugin-version>2.4</war-plugin-version>
<compiler-plugin-version>3.1</compiler-plugin-version>
<!-- dependency versions -->
<javaee-api-version>7.0</javaee-api-version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.3.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.1.Final</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>my.group.id</groupId>
<artifactId>database</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>my.group.id</groupId>
<artifactId>modele</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>my.group.id</groupId>
<artifactId>WS</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<!-- compiler plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin-version}</version>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>${war-plugin-version}</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
模型 - pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>my.group.id</groupId>
<artifactId>parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>modele</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
</project>
我探索了几种解决方案,包括:
- 更改 persistence.xml 文件的位置
- 更改提供商 (org.hibernate.jpa.HibernatePersistenceProvider)
- 使用 c3p0 提供程序
- 包括target/modele-1.0.jar到persistence.xml
- 包括 persistence.xml
我解决了我的问题:我使用的是 Hibernate 5.3 和 JPA 2.1。根据 Hibernate 文档,您可以使用带有 JPA 2.2 的 Hibernate 5.3 或带有 JPA 2.1 的 Hibernate 5.2。所以我迁移到 Hibernate 5.2,我不再有这个错误。
我有以下项目结构,它是一个 multi-module maven 项目。我的问题出在持久性配置中(位于模型模块中)。它生成我的 war 而没有返回错误,但是在实例化 EntityManager 时,它会抛出一个 javax.persistence.PersistenceException: No Persistence provider for EntityManager named myPersistence
PARENT
|
|-DATABASE
| └ src/main/
| └ pom.xml
|
|-MODEL
| |- src/main
| | └ java/
| | └ .../JPAUtil.java
| | └ ressources
| | └ META-INF
| | └ persistence.xml
| └ pom.xml
|
|-WS
| └ src/main/
| └ pom.xml
|
└ pom.xml
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="myPersistence" >
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/mySchema" />
<property name="javax.persistence.jdbc.user" value="user" />
<property name="javax.persistence.jdbc.password" value="password" />
</properties>
</persistence-unit>
</persistence>
PARENT - 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>my.group.id</groupId>
<artifactId>parent</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<modules>
<module>modele</module>
<module>database</module>
<module>WS</module>
</modules>
<properties>
<!-- encoding-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--java version -->
<java-version>1.7</java-version>
<!-- plugin versions -->
<war-plugin-version>2.4</war-plugin-version>
<compiler-plugin-version>3.1</compiler-plugin-version>
<!-- dependency versions -->
<javaee-api-version>7.0</javaee-api-version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.3.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.1.Final</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>my.group.id</groupId>
<artifactId>database</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>my.group.id</groupId>
<artifactId>modele</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>my.group.id</groupId>
<artifactId>WS</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<!-- compiler plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin-version}</version>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>${war-plugin-version}</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
模型 - pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>my.group.id</groupId>
<artifactId>parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>modele</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
</project>
我探索了几种解决方案,包括:
- 更改 persistence.xml 文件的位置
- 更改提供商 (org.hibernate.jpa.HibernatePersistenceProvider)
- 使用 c3p0 提供程序
- 包括target/modele-1.0.jar到persistence.xml
- 包括 persistence.xml
我解决了我的问题:我使用的是 Hibernate 5.3 和 JPA 2.1。根据 Hibernate 文档,您可以使用带有 JPA 2.2 的 Hibernate 5.3 或带有 JPA 2.1 的 Hibernate 5.2。所以我迁移到 Hibernate 5.2,我不再有这个错误。