- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这不是一个重复的问题。正如这个问题的正文中所解释的那样,出于各种原因,我不得不返回到一个旧的 Spring 和 Hibernate 项目。该项目使用这两个框架通过 Apache Tomcat 连接到 Apache Derby 数据库,并在去年遇到了依赖性问题,描述如下:
Error: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey with Spring controller
但同样的问题在同一个项目中再次出现,但是应用了去年的修复。
所以,这不是一个重复的问题。它询问为什么一个项目中以前解决的问题在以前修复后再次发生,以及现在使用的项目版本和遇到相同问题的版本包括去年的修复。我不明白为什么。
当我尝试构建去年的固定项目时,出现异常:
used by: java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey;
因为两个数据库表之间是一对多的关系。异常似乎指向调度程序 servlet 存储库 bean 引用:
<bean id="bookDAOImpl" class="library.dao.BookDAOImpl" />
如所示:
Could not autowire field: private org.hibernate.SessionFactory library.dao.BookDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/classes/library-servlet.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey;
在堆栈跟踪中。
在调度程序 servlet 中,我在无法创建 bean 的地方使用。
servlet如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<!-- Uses annotations in classes for JavaBeans. XML is an alternative. -->
<mvc:annotation-driven />
<!-- Base package of Library. -->
<context:component-scan base-package="library" />
<!-- Model. -->
<bean id="person" class="library.model.Person" />
<bean id="book" class="library.model.Book" />
<!-- Spring Controllers. -->
<bean id="adminController" class="library.controller.admin.AdminController" />
<bean id="personController" class="library.controller.PersonController" />
<bean id="bookController" class="library.controller.BookController" />
<bean id="exceptionController" class="library.controller.ExceptionController" />
<!-- Spring Interceptors. -->
<mvc:interceptors>
<bean id="clientInterceptor" class="library.interceptor.ClientInterceptor" />
</mvc:interceptors>
<!-- Spring Services. -->
<bean id="personService" class="library.service.PersonService" />
<bean id="bookService" class="library.service.BookService" />
<!-- Spring Repositories. -->
<bean id="personDAOImpl" class="library.dao.PersonDAOImpl" />
<bean id="bookDAOImpl" class="library.dao.BookDAOImpl" />
<!-- Spring Validators. -->
<bean id="personValidator" class="library.validator.PersonValidator" />
<bean id="bookValidator" class="library.validator.BookValidator" />
<!-- Spring ViewResolver. -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<!-- Spring MesssageSource. -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename">
<value>/WEB-INF/classes/messages</value>
</property>
</bean>
<!-- Spring Properties file for Library. -->
<bean id="propertiesFactory" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location">
<value>classpath:library.properties</value>
</property>
</bean>
<!-- Hibernate DataSource. -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" />
<property name="url" value="jdbc:derby://localhost:1527/Library" />
<property name="username" value="username" />
<property name="password" value="password" />
</bean>
<!-- Hibernate Interceptors. -->
<bean id="serverInterceptor" class="library.interceptor.ServerInterceptor" />
<!-- Hibernate SessionFactory. -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<!-- What to do with the database schema. -->
<prop key="hbm2ddl.auto">update</prop>
<!-- validate: validate the schema, makes no changes to the database.
update: update the schema.
create: creates the schema, destroying previous data.
create-drop: drop the schema at the end of the session. -->
</props>
</property>
<property name="entityInterceptor">
<ref bean="serverInterceptor" />
</property>
<property name="packagesToScan">
<list>
<value>library.model</value>
</list>
</property>
</bean>
<!-- Hibernate TransactionManagment. -->
<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
我去年遇到过这个问题,通过改变依赖关系解决了这个问题,如下所述:
Error: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey with Spring controller`但是,该项目的依赖关系自那以后没有改变,其配置也没有改变,所以我不明白为什么我会遇到这个问题。
与去年一样,该项目使用 Spring 4.0.2 和 Hibernate 4.3.5,在使用 Apache Tomcat 8.0.3.0 的 NetBeans 8 中运行。
在 Java 类中,一个名为 Person 的类与另一个名为 Book 的类之间存在 1:M 的关系。也为这些声明了数据库表。
Person类如下:
package library.model;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="PERSON")
public class Person implements Serializable {
// Attributes.
@Id
@Column(name="PERSON_ID", unique=true, nullable=false)
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer personId;
@Column(name="NAME", nullable=false, length=50)
private String name;
@Column(name="ADDRESS", nullable=false, length=100)
private String address;
@Column(name="TELEPHONE", nullable=false, length=10)
private String telephone;
@Column(name="EMAIL", nullable=false, length=50)
private String email;
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="person")
@JsonManagedReference
private List<Book> books;
// Constructors.
public Person() {
this.personId = 0;
this.name = ("");
this.address = ("");
this.telephone = ("");
this.email = ("");
this.books = null;
}
public Person(Integer personId, String name, String address, String telephone, String email, ArrayList<Book> books) {
this.personId = personId;
this.name = name;
this.address = address;
this.telephone = telephone;
this.email = email;
this.books = books;
}
使用 getter 和 setter。
本书有:
package library.model;
import com.fasterxml.jackson.annotation.JsonBackReference;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="BOOK")
public class Book implements Serializable {
// Attributes.
@Id
@Column(name="BOOK_ID", unique=true, nullable=false)
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer bookId;
@Column(name="AUTHOR", nullable=false, length=50)
private String author;
@Column(name="TITLE", nullable=false, length=50)
private String title;
@Column(name="DESCRIPTION", nullable=false, length=500)
private String description;
@Column(name="ONLOAN", nullable=false, length=5)
private String onLoan;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="person_id")
@JsonBackReference
private Person person;
// Constructors.
public Book() {
this.bookId= 0;
this.author = ("");
this.title = ("");
this.description = ("");
this.onLoan = "false";
}
public Book(Integer bookId, String author, String title, String description, String onLoan) {
this.bookId = 0;
this.author = author;
this.title = title;
this.description = description;
this.onLoan = onLoan;
}
BookDAOImpl 类是这样设置的:
package library.dao;
import java.util.List;
import library.model.Book;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository("bookDAOImpl")
public class BookDAOImpl implements GenericDAO<Book> {
@Autowired
private SessionFactory sessionFactory;
static final Logger logger = Logger.getLogger(BookDAOImpl.class.getName());
public BookDAOImpl() {
}
@Override
public void delete(Integer bookId) {
logger.info(BookDAOImpl.class.getName() + ".delete() method called.");
Session session = sessionFactory.openSession();
Transaction transaction = session.getTransaction();
try {
transaction.begin();
session.delete((Book) session.get(Book.class, bookId));
transaction.commit();
}
catch(RuntimeException e) {
transaction.rollback();
throw e;
}
finally {
session.close();
}
}
但是为什么在所有配置都没有改变的情况下,我现在得到这个?
项目中的库如下:
24 Jun 2015 23 35 445,288 antlr-2.7.7.jar
24 Jun 2015 23 35 4,467 aopalliance-1.0.jar
24 Jun 2015 23 35 160,519 commons-dbcp-1.4.jar
24 Jun 2015 23 35 62,050 commons-logging-1.1.3.jar
24 Jun 2015 23 35 2,703,892 derby-10.9.1.0.jar
24 Jun 2015 23 35 535,985 derbyclient-10.9.1.0.jar
24 Jun 2015 23 35 313,898 dom4j-1.6.1.jar
24 Jun 2015 23 35 75,311 hibernate-commons-annotations-4.0.4.Final.jar
24 Jun 2015 23 35 5,230,007 hibernate-core-4.3.5.Final.jar
24 Jun 2015 23 35 113,371 hibernate-jpa-2.1-api-1.0.0.Final.jar
24 Jun 2015 23 35 38,605 jackson-annotations-2.4.0.jar
24 Jun 2015 23 35 225,306 jackson-core-2.4.1.jar
24 Jun 2015 23 36 228,552 jackson-core-asl-1.9.7.jar
24 Jun 2015 23 35 1,074,275 jackson-databind-2.4.1.jar
24 Jun 2015 23 35 786,084 jackson-mapper-lgpl-1.9.13.jar
24 Jun 2015 23 35 76,551 jandex-1.1.0.Final.jar
24 Jun 2015 23 35 714,194 javassist-3.18.1-GA.jar
24 Jun 2015 23 35 162,126 javax.persistence-2.1.0.jar
24 Jun 2015 23 35 57,183 jboss-logging-3.1.3.GA.jar
24 Jun 2015 23 35 11,558 jboss-logging-annotations-1.2.0.Beta1.jar
24 Jun 2015 23 35 27,717 jboss-transaction-api_1.2_spec-1.0.0.Final.jar
24 Jun 2015 23 35 20,682 jstl-1.1.2.jar
24 Jun 2015 23 35 15,071 jta-1.1.jar
24 Jun 2015 23 35 367,444 log4j-1.2.14.jar
24 Jun 2015 23 35 52,150 persistence-api-1.0.jar
24 Jun 2015 23 36 36,364 spring-annotation-base-1.0.2.jar
24 Jun 2015 23 35 352,730 spring-aop-4.0.2.RELEASE.jar
24 Jun 2015 23 35 669,044 spring-beans-4.0.2.RELEASE.jar
24 Jun 2015 23 35 974,272 spring-context-4.0.2.RELEASE.jar
24 Jun 2015 23 35 960,994 spring-core-4.0.2.RELEASE.jar
24 Jun 2015 23 35 204,780 spring-expression-4.0.2.RELEASE.jar
24 Jun 2015 23 35 419,614 spring-jdbc-4.0.2.RELEASE.jar
24 Jun 2015 23 35 366,844 spring-orm-4.0.2.RELEASE.jar
24 Jun 2015 23 35 248,204 spring-tx-4.0.2.RELEASE.jar
24 Jun 2015 23 35 665,015 spring-web-4.0.2.RELEASE.jar
24 Jun 2015 23 35 660,329 spring-webmvc-4.0.2.RELEASE.jar
24 Jun 2015 23 35 393,259 standard-1.1.2.jar
最佳答案
你得到这个异常是因为在类路径的上层某处有一个旧版本的 javax.persistence
API。
从类路径中删除旧版本,你会没事的。
关于java - 错误 : java. lang.NoSuchMethodError : javax. persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31029840/
在 Tomcat 6/Ubuntu 12.04 上启动 Grails 2.1.0 应用程序时出现以下错误。 Error 500 - Internal Server Error. groovy.lang
在运行 Storm 拓扑时,我收到此错误。拓扑完美运行 5 分钟,没有任何错误,然后失败。我正在使用 Config.TOPOLOGY_TICK_TUPLE_FREQ_SECS as 300 sec i
我有一个 jsp 代码在其中一台机器上运行良好。但是当我复制到另一台机器时,我得到了这个 no such method found 异常。我是 Spring 的新手。有人可以解释我错过了什么吗? 以下
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我的代码在下面给出了一个错误; Exception in thread "main" java.lang.NoSuchMethodError: com/myApp/Client.cypherCBC(L
我正在尝试一个 Restful web 服务示例,所以当我要访问 url 时,我遇到了异常 java.lang.NoSuchMethodError: jersey.repackaged.com.goo
我正在将一个 Spring web 项目转换为一个 Maven 项目,但我收到了这个错误: java.lang.NoSuchMethodError: org.jboss.logging.Logger.
在我的项目中,我有一个像这样的枚举: public enum MyEnum { FIRST(1), SECOND(2); private int value; private MyEnum(int v
我创建了这个简单的示例,用于读取 Linux 正常运行时间: public String getMachineUptime() throws IOException { String[] di
我正在使用 Eclipse,并且正在使用 Java。我的目标是使用 bogoSort 方法对 vector 进行排序在一个 vector (vectorExample)中适应我的 vector 类型,
我正在运行以下查询。它显示一条错误消息。如何解决这个错误? ListrouteList=null; List companyList = session.createS
我有以下模型类: @Entity @Table(name="user_content") @org.hibernate.annotations.NamedQueries({ @org.
我有那个错误。这是我的代码: GmailSettingsService service = new GmailSettingsService(APPLICATION_NAME, DOMAIN_NAME
实际上我在执行我的java程序时遇到了下面提到的错误 Exception in thread "pool-1-thread-1" java.lang.ClassCastException: jav
java.lang.ClassCastException: java.lang.Float cannot be cast to java.lang.String 我在以下代码中遇到此异常: Strin
我正在尝试从 linkedhashset 中检索随机元素。下面是我的代码,但它每次都给我异常。 private static void generateRandomUserId(Set userIds
我已经完成了 Android 中的代码: List spinnerArray = new ArrayList(); for (int i = 0; i item = (LinkedTreeMap)
这个问题已经有答案了: Explanation of ClassCastException in Java (12 个回答) 已关闭 6 年前。 我已经编写了 java 到 Json 的代码,同时从页
这个问题在这里已经有了答案: ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn (4 个答案) 关闭 6 年前
我在运行时遇到问题来编译这段代码,这给我一个错误,java.lang.Integer 无法转换为 Java.lang.Double。如果有人帮助我更正此代码,我将非常高兴 double x; pu
我是一名优秀的程序员,十分优秀!