Home » Java » Enterprise Java » What is Spring Data?

About Bear Giles

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.
 

Reference: What is Spring Data? from our JCG partner Bear Giles at the Invariant Properties blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

 

3 comments

  1. Do you mean Mongo DB? I don’t know NoSQL databases name as Mondo :)

  2. 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.

  3. 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

Leave a Reply

Your email address will not be published. Required fields are marked *

*


Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.