gpt4 book ai didi

java - EntityManager Hibernate 5 没有持久性提供程序

转载 作者:行者123 更新时间:2023-11-30 06:09:20 25 4
gpt4 key购买 nike

我有以下项目结构,它是一个多模块 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>

父级 - 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,不再出现此错误。

关于java - EntityManager Hibernate 5 没有持久性提供程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50607837/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com