正如您所指出的,Java 8 及其附加语法与 OpenJPA 2.3.0 类增强不兼容。但是,如果您绝对需要使用为 Java 8标准库编写的 OpenJPA 2.3.0 代码,您仍然可以使用输出 Java 版本 <8 字节码的 Java 8 JDK 编译您的代码。例如,我在 Maven 中使用此解决方法:
聚甲醛:
<?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>com.stackoverflow.examples</groupId>
<artifactId>openjpa-2.3.0-jdk8</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>openjpa-2.3.0-jdk8</name>
<url>http://stackoverflow.com/a/29343171/1391325</url>
<properties>
<!-- NOTE: As of OpenJPA version 2.3.0, Java 8 is still unsupported -->
<javac.version>1.7</javac.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<openjpa.version>2.3.0</openjpa.version>
<openjpa.entityDir>com/stackoverflow/examples/persistence/entities</openjpa.entityDir>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa</artifactId>
<version>${openjpa.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>${javac.version}</source>
<target>${javac.version}</target>
<!-- NOTE: The variable "JAVA_8_HOME" must be set either in the Maven
settings.xml file or as an environment variable! -->
<executable>${JAVA_8_HOME}/bin/javac</executable>
<fork>true</fork>
</configuration>
</plugin>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>enforce-property</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireProperty>
<property>JAVA_8_HOME</property>
<message>Property "JAVA_8_HOME" not set.</message>
</requireProperty>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa-maven-plugin</artifactId>
<version>${openjpa-maven-plugin.version}</version>
<configuration>
<includes>${openjpa.entityDir}/*.class</includes>
</configuration>
<executions>
<execution>
<id>enhancer</id>
<phase>process-classes</phase>
<goals>
<goal>enhance</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa</artifactId>
<!-- set the version to be the same as the level in your runtime -->
<version>${openjpa.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
设置.xml:
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<profiles>
<profile>
<id>java-8-home</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<JAVA_8_HOME>/usr/lib/jvm/java-8-oracle/</JAVA_8_HOME>
</properties>
</profile>
</profiles>
</settings>