打包Maven项目持久性后不起作用

时间:2019-06-06 09:58:01

标签: java maven build

运行程序时,我的程序运行正常。当我运行maven程序包并开始运行.jar文件时,出现javax.persistence.Persistence错误。我尝试使用其他休眠版本,但无法正常工作。谢谢你的帮助:)

我的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>

    <groupId>hu.nyilvantarto</groupId>
    <artifactId>Nyilvantarto</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <property>
        </property>
    </properties>


    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.4.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.4.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
            <version>2.4.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <version>10.14.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.2.3.Final</version>
        </dependency>

        <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
            <version>2.4.1</version>
        </dependency>


        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.10</version>
        </dependency>


    </dependencies>



    <build>

        <resources>
            <resource>
                <filtering>false</filtering>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.fxml</include>
                </includes>
            </resource>
        </resources>



        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>


            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                 <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>sample.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

        </plugins>
    </build>


</project>

我的persistence.xml文件位于resources / META-INF文件夹中:


<persistence 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"
             version="2.1">
    <persistence-unit name="nyilvantartoDatabasePU">
    <properties>


        <property name="javax.persistence.jdbc.url" value="jdbc:mariadb://***.***.**.***:3306/database"/>
        <property name="javax.persistence.jdbc.user" value="username"/>
        <property name="javax.persistence.jdbc.password" value="password"/>
        <property name="javax.persistence.jdbc.driver" value="org.mariadb.jdbc.Driver"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
        <property name="hibernate.hbm2ddl.auto" value="update"/>

    </properties>



    </persistence-unit>
</persistence>

像这样的数据提供者:

 private EntityManagerFactory emf;

    public DataProvider(){
        emf= Persistence.createEntityManagerFactory("nyilvantartoDatabasePU");
    }

错误是:

Caused by: java.lang.NoClassDefFoundError: javax/persistence/Persistence
    at database.DataProvider.<init>(DataProvider.java:26)
    at dolgozo.dolgozolista.Dolgozolista.refreshTable(Dolgozolista.java:78)
    at dolgozo.dolgozolista.Dolgozolista.initialize(Dolgozolista.java:69)
    ... 27 more
Caused by: java.lang.ClassNotFoundException: javax.persistence.Persistence

1 个答案:

答案 0 :(得分:0)

由于您使用的是休眠模式,因此仅实体管理器是不够的。您需要将以下内容添加到pom中:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.2.3.Final</version>
</dependency>