NoSuchMethodError:javax.persistence.JoinColumn.foreignKey()

时间:2014-11-22 11:51:35

标签: java hibernate maven jpa openshift

我试图在OpenShift(JBoss企业应用程序平台6)上部署我的应用程序,并且我得到了标题中提到的NoSuchMethodError。所以我搜索了整个事情并发现了一些文章或线程,提到自JPA-API 2.1以来这个方法是可用的。我发现Openshift所需的依赖包含JPA-API 2.0。我试图排除这种依赖,但它不起作用。有任何想法吗?请在下面找到pom.xml。

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>christmasmarkets</groupId>
<artifactId>christmasmarkets</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>christmasmarkets</name>
<repositories>
    <repository>
        <id>eap</id>
        <url>http://maven.repository.redhat.com/techpreview/all</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>springsource-repo</id>
        <name>SpringSource Repository</name>
        <url>http://repo.springsource.org/release</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>eap</id>
        <url>http://maven.repository.redhat.com/techpreview/all</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
    <spring.version>4.1.2.RELEASE</spring.version>
    <hibernate.version>4.3.7.Final</hibernate.version>
    <hibernate-annotations.version>3.5.6-Final</hibernate-annotations.version>
    <hibernate-commons-annotations.version>3.2.0.Final</hibernate-commons-annotations.version>
    <jackson.version>2.2.3</jackson.version>
    <postgre.version>9.3-1102-jdbc41</postgre.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.jboss.spec</groupId>
        <artifactId>jboss-javaee-6.0</artifactId>
        <version>3.0.2.Final-redhat-4</version>
        <type>pom</type>
        <scope>provided</scope>
        <exclusions>
            <exclusion>
                <groupId>org.hibernate.javax.persistence</groupId>
                <artifactId>hibernate-jpa-2.0-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>${postgre.version}</version>
    </dependency>

</dependencies>
<profiles>
    <profile>
        <!-- When built in OpenShift the 'openshift' profile will be used when 
            invoking mvn. -->
        <!-- Use this profile for any OpenShift specific customization your app 
            will need. -->
        <!-- By default that is to put the resulting archive into the 'deployments' 
            folder. -->
        <!-- http://maven.apache.org/guides/mini/guide-building-for-different-environments.html -->
        <id>openshift</id>
        <build>
            <finalName>christmasmarkets</finalName>
            <plugins>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.1.1</version>
                    <configuration>
                        <outputDirectory>deployments</outputDirectory>
                        <warName>ROOT</warName>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

2 个答案:

答案 0 :(得分:2)

这里的问题是容器在运行时提供了JPA 2.0 。在pom.xml中排除提供的依赖项仅在编译时有效。要解决此问题,您必须在容器中配置类加载。

OpenShift JavaEE 6支持基于JBoss AS 7.来自Class Loading in AS7文档:

  

jboss-deployment-structure.xml是JBoss特定的部署   描述符,可用于控制细粒度的类加载   方式。它应该放在META-INF的顶级部署中   (或用于Web部署的WEB-INF)。它可以执行以下操作:

   Prevent automatic dependencies from being added
   Add additional dependencies
   ...

因此,您需要添加自定义jboss-deployment-structure.xml文件,以排除hibernate-jpa-2.0-api自动添加。

基于OpenShift's documentation

<jboss-deployment-structure>
   <deployment>
      <exclusions>
         <module name="javax.persistence.api" />
      </exclusions>
   </deployment>
</jboss-deployment-structure>

答案 1 :(得分:0)

将此Maven依赖项添加到您的pom.xml:

<dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.1-api</artifactId>
    <version>1.0.0.Final</version>
</dependency>