Imagine a bacon-wrapped Ferrari. Still not better than our free technical reports.
See all our reports

State of the Art Data Access with Spring Data by Oliver Gierke

oliver-gierke

The last vJUG session in June was all about Spring Data project and how it can make your life easier by unifying and simplifying the way your code interacts with the database. The session was special because we had Oliver Gierke from Pivotal for the speaker. Oliver is the lead of the Spring Data project and no doubt is in the best position to describe how the project is shaping up and why it’s happening. Oliver delivered a great and educational session and if you didn’t get a chance to ask questions on the IRC, which is by the way, #VIRTUALJUG at Freenode, you can always catch up with him on Twitter: @olivergierke.

Anyway, let’s dig into what we have learnt from this session. If you want to watch it for yourself first, here it is embedded for your convenience.

At the same time, here are the main takeaways from the first part of the session.

Spring Data is an umbrella project that provides you with easy to use data access technologies for all kinds of relational and non-relational databases. Sounds vague, doesn’t it? But if we think of the database as a resource that we have to consume, we realize that from inside the application database looks like everything else, an entity behind some kind of API facade.

There are multiple technologies that give you APIs for database access and they differ in terms of ease of use, convenience, expressiveness, and correctness after all. For example the lowest possible level of database access in the Java world is, probably, JDBC.

If we think of what JBDC offers you in terms of API, we find three main features it gives you:

  • ability to prepare SQL statements
  • ability to execute statements
  • exposes a result set for iteration

This is the lowest level that an API can provide, everything you do you have to do manually. On top of JDBC there are other technologies, like Java Persistence API, JPA, that make your life easier with a more convenient, albeit more error prone API. JPA has standardized object relational mappings, introduced EntityManagers to manage transactions and objects inside a database session and so forth. What JPA gives you is that you don’t have to deal with raw SQL queries anymore.

In the same fashion, several non-relational databases offer Java drivers that expose an API to communicate with the data store.

spring-data-tech-stack

Spring Data is a layer that intends to unify the API to access, so for example you can use your MongoDB driver wrapped into the Spring Data utilities in the same way as you would use Redis driver. The exact vocabulary for both will differ, but the configuration and the main elements you program interacts with will be the same.

Internally, Spring Data architecture is highly modularized, it has several core modules that are maintained by the core contributors, and that sets the tone for the API to all those datastore technologies. Individual contributors can easily contribute their own modules and there are numerous modules available already.

spring-data-modules

All of the common modules are released simultaneously

Spring Data Module Structure

If we look further into a Spring Data module, we see the common structure. First of all the module requires an object – database view mapping. For JPA, the object mapping is provided, but all other modules have to provide one. Basically, the mapping converts plain Java objects into whatever format the database deems appropriate: a document for MongoDB, a graph in Neo4j world, a set of properties in Redis and so forth.

spring-data-module-structure

On top of these mappings, Spring Data module builds a template. You might know the JDBCTemplate from SpringFramework, then you already recognize that main responsibilities of the template are resource management and exceptions translations. All templates in every module do the same thing, but they deal with different APIs and they do not provide a unified API further as well.

However, every module on top of the templates provides a repository. The repository includes the common way of querying data from the data store. If it is reasonable, a repository also provides support for basic CRUD operations for entities.
Also, being a Spring project, Spring Data takes extra effort to be easily configurable and working out of the box with Spring Boot or any other way of configuring a Spring application.

I hope you have heard enough about Spring Data that you will now watch and enjoy the practical part Oliver. Oliver naturally has dealt with the internals of the project long enough to achieve the mastery of it and the demo.

How to create and configure repositories? What is the common way to set up tests and how do different databases work under Spring Data? These are just a few of the questions that Oliver has covered. Watch the video above, it’s really interesting.

Interview and final words

When the demo finished and the questions from the IRC were all answered, we got Oliver to answer a couple of questions for us about Java development, Spring future, and how to organize tests in a low level library project that is used by thousands of people. We asked what he thinks about the Eclipse Mars release and how one can grow as a developer.

The video of our quick chat is right below, watch it, ping Oliver on Twitter: @olivergierke, and tell him or us (@ZeroTurnaround) what you think.

So, are you using Spring Data or would you pick it up for your next project? Leave us a comment below or

If you leave your email in the form below, I’d be happy to send you an occasional email about what’s happening with RebelLabs, the best pieces of content we have and what’s happening in the world of Java in general.

 


Read next: