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

时间:2018-05-30 14:57:24

标签: java hibernate jpa persistence jpa-2.2

我有以下项目结构,这是一个多模块maven项目。我的问题在于持久性配置(位于模型模块中)。它生成我的战争而不返回错误但是当实现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>

MODELE - 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

1 个答案:

答案 0 :(得分:1)

我解决了我的问题:我在使用带有JPA 2.1的Hibernate 5.3。根据Hibernate文档,您可以使用带有JPA 2.2的Hibernate 5.3或带有JPA 2.1的Hibernate 5.2。所以我迁移到Hibernate 5.2,我不再有这个错误。