JPA / Hibernate java.lang.NoSuchMethodError

时间:2015-10-21 01:13:42

标签: java hibernate maven jpa

错误消息

  

2015年10月20日下午10:56:33 org.hibernate.dialect.Dialect INFO:   HHH000400:使用方言:org.hibernate.dialect.PostgreSQL9Dialect 10月   20,2015 10:56:33 PM   org.hibernate.engine.jdbc.internal.LobCreatorBuilder   useContextualLobCreation信息:HHH000424:禁用上下文LOB   创建为createClob()方法时抛出错误:   java.lang.reflect.InvocationTargetException线程“main”中的异常   java.lang.NoSuchMethodError:   javax.persistence.JoinColumn.foreignKey()Ljavax /持久/ ForeignKey的;

我知道这是several questions here about this,但正如大多数人所说,他们要求确保我使用的是hibernate 4.3+和JPA2。 1+。我相信我正在使用它们两个:

这是我的 persistence.xml

<persistence version="2.1" 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">
  <persistence-unit name="web" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/web"/>
      <property name="javax.persistence.jdbc.user" value="postgresUser"/>
      <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
      <property name="javax.persistence.jdbc.password" value="admin"/>
      <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
      <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
      <property name="hibernate.ejb.entitymanager_factory_name" value="web" />
    </properties>
  </persistence-unit>
</persistence>

这是我的 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>edu.utfpr</groupId>
<artifactId>ProjetoWeb</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>ProjetoWeb</name>

<properties>
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.torpedoquery</groupId>
        <artifactId>org.torpedoquery</artifactId>
        <version>1.7.0</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-jpamodelgen</artifactId>
        <version>5.0.2.Final</version>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.2-1002-jdbc4</version>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <compilerArguments>
                    <endorseddirs>${endorsed.dir}</endorseddirs>
                </compilerArguments>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${endorsed.dir}</outputDirectory>
                        <silent>true</silent>
                        <artifactItems>
                            <artifactItem>
                                <groupId>javax</groupId>
                                <artifactId>javaee-endorsed-api</artifactId>
                                <version>7.0</version>
                                <type>jar</type>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
</project>

最后这里是BarEntity的一个例子

@Entity
public class BarEntity implements Serializable {

    @Id
    @GeneratedValue
    private long id;

    @ManyToOne
    @JoinColumn(name = "fooId", referencedColumnName = "id")
    @Cascade({CascadeType.SAVE_UPDATE})
    private Foo foo;

    @Column(nullable = false, unique = true)
    private String bar;

    @Column(nullable = false)
    private String foobar;
}

我可以做些什么来部署它?

我很抱歉无法将pom.xml / persistence.xml缩减为更通用的,因为我不确定错误在哪里。

2 个答案:

答案 0 :(得分:3)

这通常意味着您使用的库版本与您真正依赖的库版本不兼容。请检查javax.persistence.JoinColumn.foreignKey()库的版本或正在使用此库的版本,以确保使用它们的正确版本。

答案 1 :(得分:3)

正如enter image description hereZhao所述,这绝对是一个依赖问题。但是,我相信没有经验的人可能难以确定哪些应该匹配/可能是错误的。

所以,这是一个如何检查

的方法

pom.xml中的相关数据是:

    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.3.1.Final</version>
    (...)
    <artifactId>hibernate-jpa-2.1-api</artifactId>
    <version>1.0.0.Final</version>
    (...)
    <artifactId>hibernate-jpamodelgen</artifactId>
    <version>5.0.2.Final</version>

正如用户所说,他说得对,hibernate必须是4.3或更高。但是,它应该使用相同版本的hibernate和jpamodelgen。

hibernate-entitymanager hibernate-jpamodelgen 应该具有相同的数字。是的:

4.3.15.0.2