持久性单元被覆盖

时间:2017-08-29 15:35:40

标签: java maven jpa persistence.xml persistence-unit

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

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

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

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

Maven构建过程成功完成,但是当运行jar时,我得到一个例外:

  

线程“main”中的异常javax.persistence.PersistenceException:否   尝试后,持久性提供程序可用于“PersistenceUnitXXX”   以下发现的实现:   org.eclipse.persistence.jpa.PersistenceProvider

此外,包含Entity类的项目的持久性单元用于在应用程序服务器上进行部署,因此我认为不可能将该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

1 个答案:

答案 0 :(得分:0)

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

Maven overwrite resource file in dependency