gpt4 book ai didi

java - 打包后maven项目持久化不起作用

转载 作者:行者123 更新时间:2023-12-02 04:46:57 25 4
gpt4 key购买 nike

当我运行我的程序时,工作完美。当我运行 Maven 包并开始运行 .jar 文件时,出现 javax.persistence.Persistence 错误。我尝试了其他 hibernate 版本但不起作用。谢谢你帮助我:)

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

最佳答案

由于您使用的是 hibernate,仅实体管理器是不够的;您需要将以下内容添加到您的 pom 中:

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

关于java - 打包后maven项目持久化不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56475173/

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