gpt4 book ai didi

java - 持久性单元被覆盖

转载 作者:太空宇宙 更新时间:2023-11-04 11:11:39 25 4
gpt4 key购买 nike

我正在编写一个独立的 java 应用程序,从 Maven 项目构建它并通过调用 jar 文件来执行。

在应用程序中,我有一个使用持久单元的实体管理器,在我的 persistence.xml 中定义为独立的(transaction-type="RESOURCE_LOCAL")

实际的实体注释类来自另一个项目,因此作为依赖项添加到 pom.xml 文件中。

问题在于,包含实体类的项目的 persistence.xml 在构建时会覆盖 jar 目标文件夹中实际独立应用程序的 persistence.xml。

Maven 构建过程成功完成,但运行 jar 时出现如下异常:

Exception in thread "main" javax.persistence.PersistenceException: No persistence providers available for "PersistenceUnitXXX" after trying the following discovered implementations: org.eclipse.persistence.jpa.PersistenceProvider

此外,包含实体类的项目的持久性单元旨在部署在应用程序服务器上,因此我认为不可能为独立应用程序重用该 persistence.xml。

pom 看起来像这样:

<?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>com.my</groupId>
<artifactId>projects</artifactId>
<version>1.0</version>
<relativePath>../..</relativePath>
</parent>

<groupId>com.my.projects</groupId>
<artifactId>mystandaloneapp</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>com.oracle.weblogic</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.3-0-0</version>
</dependency>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0-5</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20170516</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.16</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.my</groupId>
<artifactId>my_entities</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.my.StandAloneApp</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>create-my-bundle</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>eksport-assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

文件结构为

-projects       
-mystandaloneapp
-src
-main
-java
-com
-my
-db
-EntityConsumer
-resources
-WEB-INF
-persistence.xml
-my_entities
-src
-main
-java
-com
-my
-model
-db
-MyEntity
-resources
-WEB-INF
-persistence.xml

最佳答案

好吧,这个问题是这个问题的重复,它提供了一个可行的解决方案:

Maven overwrite resource file in dependency

关于java - 持久性单元被覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45943148/

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