gpt4 book ai didi

java - JPA/hibernate java.lang.NoSuchMethodError

转载 作者:行者123 更新时间:2023-12-02 08:24:46 25 4
gpt4 key购买 nike

错误信息

Oct 20, 2015 10:56:33 PM org.hibernate.dialect.Dialect INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL9Dialect Oct 20, 2015 10:56:33 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException Exception in thread "main" java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/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 简化为更通用的内容,因为我不确定错误在哪里。

最佳答案

正如 Zhao 所说和 hermitmaster这绝对是一个依赖问题。但是,我相信对于经验不足的人来说,很难弄清楚什么应该匹配/可能是错误的。

所以,这是检查这个的方法

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-entitymanagerhibernate-jpamodelgen 应该有相同的编号。就这样吧:

4.3.15.0.2

关于java - JPA/hibernate java.lang.NoSuchMethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33249354/

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