I have an existing project where I wanted to start using the Spring Data JPA library to stop having to do all the code involved in a DAO. It sounded like a smart idea (without any prior knowledge) but when I tried to implement I realized that every time you try something new there are a few hurdles and unexpected surprises.
Searching for solution and tutorial with Google might be a good fallback plan but I found so many references to different ways that I got confused before sorting it out a bit. What you don’t easily see in all the solutions is which version of which library they are using until you have read most of the solution. I wish we could more easily search for examples and tutorial based on library versions.
Versions
Here is what I used for my project:
Spring Core 3.1.2.RELEASE
Spring Data 1.1.0.RELEASE
Apache Derby 10.2.2.0 (old version)
Hibernate 3.6.9 (old version)
Limitations
Since I was working within an existing project I needed to work with certain things in place that I could not remove to use the Spring Data JPA library.
I needed to work with some XML files for the application context.
I needed to work with Apache Derby (old version).
I needed to work with Hibernate (old version).
Code changes
First thing that I needed to do was to add some dependencies in my pom.xml to be able to use Spring Data JPA.
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.1.0.RELEASE</version>
</dependency>
It took me some trial and error to also realize that I needed an entity manager and since I was using Hibernate the logical choice was:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.9.Final</version>
</dependency>
I already had the Hibernate dependency in my pom.xml before starting:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.9.Final</version>
</dependency>
In my Java code I already had an entity that I wanted to use but because of the other entities that were managed under hibernate without Spring Data JPA I had to move this one to its own package so I could use some package scanning. Here is the entity code:
package com.cinq.test.domain;
import java.util.GregorianCalendar;
import javax.persistence.Column;
import javax.persistence.Entity;
@Entity
public class Event {
@Column(nullable=false)
private Long id;
@Column(nullable=false)
private String title;
@Column
private String Description;
@Column(nullable=false)
private GregorianCalendar date;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public GregorianCalendar getDate() {
return date;
}
public void setDate(GregorianCalendar date) {
this.date = date;
}
}
I needed to create a public interface to bind this entity to the Spring Data JPA “magic”. It simply needs to extend the CrudRepository and all the CRUD code is created for you.
package com.cinq.test.repository;
import org.springframework.data.repository.CrudRepository;
import com.cinq.test.domain.Event;
public interface EventRepository extends CrudRepository{
}
I also needed to add some quick configuration to my app-servlet.xml to get Spring to scan for repositories at startup:
Issues that needs to be worked out
The application runs on Tomcat but is giving an exception for a class not found on JBoss 7.1.1. I will have to look at the vfs settings for a default install.
I still need to fix the form submission.
References:
Spring Data – JPA (Starting point at SpringSource)
The Persistence Layer with Spring 3.1 and JPA (Starting point – tutorial)
Difference between configuring data source in persistence.xml and in spring configuration files (highlighted that with Spring 3.1 you don’t need the persistence.xml)
5 minutes with – Jpa transaction (highlighted the databasePlatform property in the xml configuration)
Problems with Spring maven hibernate org/hibernate/ejb/HibernatePersistence (need for hibernate-entity-manager)
13. Data access with JDBC (reference material for many items)