I am using Spring 4.0.2 and Hibernate 4.0.35 in a simple webapp which uses Apache Tomcat 8.0.3.0 to talk to an Apache Derby database.
However, when I try to build my project, it fails because of:
Code:
Error: java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey;
Which is pointing at:
Code:
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;
In the stack trace And I cannot understand why. As far as I am aware all the dependencies are correct because the project has run perfectly well before this issue manifested itself.
The application is a simple two Java class scenario of a Person and Books. The Person class is as follows:
Code:
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;
}
With appropriate getter and setter methods.
The matching Book class is:
Code:
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;
}
Again, with appropriate getters and setters.
BookDAOImpl is defined as:
Code:
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();
}
}
With other CRUD methods.
The Spring dispatcher servlet is:
Code:
<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>
The database SQL is:
Code:
CREATE TABLE Person (
Person_Id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
Name VARCHAR(50) NOT NULL,
Address VARCHAR(100) NOT NULL,
Telephone VARCHAR(10) NOT NULL,
Email VARCHAR(50) NOT NULL,
CONSTRAINT primary_key_person PRIMARY KEY(Person_Id)
);
And:
Code:
CREATE TABLE Book (
Book_Id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
Author VARCHAR(50) NOT NULL,
Title VARCHAR(100) NOT NULL,
Description VARCHAR(500) NOT NULL,
OnLoan VARCHAR(5) NOT NULL,
Person_Id INTEGER,
CONSTRAINT primary_key_book PRIMARY KEY(Book_Id),
CONSTRAINT foreign_key_book FOREIGN KEY(Person_Id) REFERENCES Person(Person_Id)
);
So there is nothing unusual going on.
The libraries in the NetBeans project are illustrated below:
Code:
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
Can anyone help?