What is Spring Data?
(This is related to Project Student and I will revisit this topic later.)
Spring Data came up in passing in several recent interviews. What is Spring Data?
To answer that let’s consider the standard approach to persistence – all access is made via Data Access Objects (DAOs). This completely isolates the rest of the system from the specific details of the persistence mechanisms. This sounds easy but anyone who has ever done a non-trivial project knows there’s a big headache.
DAO code is boring.
It’s monotonous, it has a lot of very similar code, and a slight error can cause a lot of damage. Worse it violates the Don’t Repeat Yourself (DRY) principle since most of the information is already captured in JPA annotations.
This is such a big problem that there have been code generation tools for years. In theory they solve the problems but in practice they introduce their own. E.g., the need for custom configuration files or annotations.
Using an interface as a DRY contract
What is the ultimate DRY contract in the Java world? That’s easy – it’s an Interface. Given an interface and a template we can use CGLib to generate the necessary classes on the fly during application startup. There’s a slight performance hit but it’s modest when compared to the benefits.
Student.java
@Entity public class Student { private Integer id; private String uuid; private String name; private String emailAddress; private Integer creditHours; @Id public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(unique=true) public String getUuid() { return uuid; } public void setUuid(String uuid) { this.uuid = uuid; } @Column public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(unique=true) public String getEmailAddress() { return emailAddress; } public void setEmailAddress(String emailAddress) { this.emailAddress; } @Column public Integer getCreditHours() { return creditHours; } public void setCreditHours(Integer creditHours) { this.creditHours = creditHours; } }
What could our Interface look like?
repository/StudentRepository.java
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface StudentRepository extends CrudRepository<Student, Integer> { // this could also be getByUuid() or fetchByUuid() - all are recognized Student findStudentByUuid(String uuid); Student findStudentByEmailAddress(String emailAddress); List<Student> findStudentsByNameLike(String pattern); // we can use a custom query @Query("select s from Student s where s.creditHours < 15") List<Student> findFreshmen(); }
and so on. In all there are 15 predicates that can be used alone or in combination.
- And
- Or
- Between
- LessThan
- GreaterThan
- IsNull
- IsNotNull
- NotNull
- Like
- NotLike
- OrderBy
- Not
- In
- NotIn
- IgnoreCase
N.B., it is not necessary to implement this interface! CGLib takes care of that for us.
Custom Methods
Sometimes we need to write our own DAO methods. They are easy to integrate into the generated code.
public interface StudentExtras { Student flogStudent(Student student); } @Repository public interface StudentRepository extends CrudRepository<Student, Integer>, <em>StudentExtras</em> { } // this class does NOT implement StudentRepository! public class StudentRepositoryImpl implements StudentExtras { public Student flogStudent(Student student) { ... } }
The custom methods must be a specific class (due to configuration by convention) but are otherwise unrestricted.
NoSQL
Spring Data also transparently supports NoSQL databases: Mondo (documents), Neo4j (graph), Redis (key-value), Hadoop (map-reduce) and GemFire.
Pagination
Finally we have the question of pagination. User interfaces typically only look at a subset of the available information, e.g., a page of 25 items. Pagination isn’t difficult, just boring and error-prone.
Spring Data supports pagination by extending the PagingAndSortingRepository interface instead of the CrudRepository interface.
Do you mean Mongo DB? I don’t know NoSQL databases name as Mondo :)
In 2008 I were using “warp persist” with Google Guice – almost identical library ( in terms of functionality)
So it seems that support for Mongo is the only innovation added by Spring team.
Hi
Spring documentation says that Spring Session can transparently leverage Redis to back a web application’s HttpSession when using REST endpoints. This seems to have been implemented using spring-data-redis
Can we develop a similar integration between Spring Session and GemFire using spring-data-gemfire ?
Ref: http://docs.spring.io/spring-session/docs/current/reference/html5/guides/rest.html