Spring Data JPA
Spring Data JPA - Deep dive.

Spring Data JPA

Spring Data JPA

What is it? If you have been using this Spring module “blindly” this article will unpack it for you. So, you understand that Spring is not doing magic.

This is part of the Spring Framework family. It is a module. From Spring Website you can see the following submodules.

No alt text provided for this image

Many acronyms that form a module. Let’s decipher the nomenclature of this spring module, explaining how they connect to form a complete whole. This will be a very rich but short article. Pretty sure, you can get some new knowledge.

First of all, it is JPA. Java Persistence API, and lately called Jakarta Persistence. Is a specification that describes management of relational data in Java development. How is this specification trusted such that it can be implemented by vendors such as RedHat, Eclipse-Foundation etc. There is a community called JCP (Java Community Process), organised by ORACLE corporation. This community consists of membership from organisations and some individuals. The members of the organisation write what is called JSRs (Java Specification Requests): actual descriptions of proposed and final specifications for the Java platform technologies. Members vote for those specifications, which will then be added into a document (JSR). As a specification, it has several implementations by different vendors. We have vendors like Hibernate, Eclipse-Link, My-Batis etc. That’s about JPA.

No alt text provided for this image
No alt text provided for this image

Now, back to Spring Data JPA. What is Spring Data?

Some developers think Spring Data is a JPA provider! This is wrong. Spring Data is not a JPA provider. Spring Pivotal team decided that they will not create their own JPA provider since there already exist a more powerful provider by RedHat which is Hibernate. So, they thought, if you can’t beat them, join them. They then decided to make Hibernate their default JPA provider. Hence you longer need to include Hibernate as a dependency with spring-boot-starter-data-jpa, as hibernate is a transitive dependency. But still Hibernate did not give them what they wanted. A developer still needed to write a descent amount of boiler plate code to write basic queries. As you all know Spring framework is innovative, and probably the best out there. I know Java EE developers might fight back, I am not Spring evangelist, but if you want to fight, I am here at Auckland park.

Now, back to the point. Spring decided, why can’t we built on top of Hibernate, add some extra level of abstraction to enhance data access, offer a developer a more sophisticated interface than the plain EntityManager, thereby allowing them to specify only interfaces or repositories and let Spring provide implementation of those at runtime through AOP. This spring does by using JdkDynamicProxy aka interface proxies. Spring creates a proxy bean of Type SimpleJpaRepository at runtime by default, that is advised by your custom repository as well as other Spring classes. The picture below shows the proxy.

No alt text provided for this image

How does Spring know your repository interfaces?

If you are using Spring boot and have spring-boot-starter-data-jpa as a dependency in your project, through spring-data auto-configurations, Spring will do component scanning and when it finds those interfaces that implicitly or explicitly extend Repository interface, it will create SimpleJpaRepository proxy bean for each of them.

Of-course you need to give spring some configurations either in property file of through Java configurations for it to obtain a datasource. Spring boot uses Hikari by default. 

Your interface must extend Repository interface explicitly or implicitly for it to qualify for selection during component-scanning. Hierarchy of Spring repository interfaces that are picked up during component scanning.

No alt text provided for this image

Explicitly extending Repository interface. But this gives you less methods.

No alt text provided for this image

Today, Spring recommends extending from JpaReposity as it provides rich functionality to access data.

No alt text provided for this image

Just by extending JpaRepository, you get a whole lot of methods inherited in the hierarchy and spring will provide implementations for them at runtime.

When you inject or autowire your repository bean into your service through constructor or setter or field injection, the Spring container (application context), will inject a proxy. How this proxy bean gets created is a talk for another article. There are types of proxies that spring can create, JdkDynamic proxies (interface proxies) and class-based proxies. Since this article is not about proxies, I won’t talk about the nitty-gritties of proxies on this article. For now, just know that SimpleJpaRepository qualifies to be an interface-based proxy and hence Spring chooses this proxy type.

 If you are not using Spring boot which does auto configuration, and you also have spring-data as a dependency, you will have to create a configuration class and enable JpaRepositories for component-scanning. The below class achieves that. You also have to provide data source configurations in your configuration class.

No alt text provided for this image

Spring Data JPA, is an extra level of abstraction added by Spring pivotal team on top of a JPA provider. Implementing a data access layer of an application has been cumbersome for quite a while. Too much boilerplate code has to be written to execute simple queries as well as perform pagination, and auditing. Spring Data JPA aims to significantly improve the implementation of data access layers by reducing the effort to the amount that’s actually needed. As a developer you write your repository interfaces, including custom finder methods, and Spring will provide the implementation automatically


I have put together some Java content to share knowledge that can benefit someone. Can you be kind to support the channel by subscribing? Below is the youtube link.

Oscar Ngxongwana

Certified Oracle Cloud Infrastructure Architect Professional

2y

Informative👌🏽

BOKANG THOOLA

Android | Kotlin | Java| Digital Transformations| DevSecOps|Writer

2y

Interesting read.

To view or add a comment, sign in

More articles by Mothusi Molorane

  • Sleep vs Wait

    Sleep vs Wait

    In Java, sleep and wait are both used to pause the execution of a thread, but they serve different purposes and have…

    1 Comment
  • The for each loop

    The for each loop

    What is a forEach loop? The syntaxic sugar to iterate over an iterable, internally the compiler creates an iterator for…

  • Deciphering Spring Boot magic

    Deciphering Spring Boot magic

    What is spring auto configuration? First, this question seems to be easy because as you know, most developers use…

  • Spring Dependency Injection

    Spring Dependency Injection

    Dependency Injection Is an action of supplying dependencies to a dependant bean. Since most of my audience are familiar…

  • MicroService All In One

    MicroService All In One

    Good morning to my LinkedIn friend. Today, I want to share my presentation slides again.

  • Spring Authorisation Server

    Spring Authorisation Server

    SPRING SECURITY “Spring Security is a framework that provides authentication, authorization, and protection against…

  • Spring Data Series 2

    Spring Data Series 2

    To my LinkedIn audience, happy Tuesday morning. Remember the list of topics we are looking at in our series.

  • JPA and Spring Data

    JPA and Spring Data

    I have already published several articles on spring data and I will be building from them. If you have not read the…

  • I had an interview with a developer

    I had an interview with a developer

    Mothusi: Hello guys, today I have a Java interview with JavaSpace (aka JS) the name of my YouTube channel. Please help…

    3 Comments
  • Answer to puzzle 3

    Answer to puzzle 3

    Remember last week I posted this code snippet below. This code seem like it will loop from 123456789 to 0 however it's…

Insights from the community

Others also viewed

Explore topics