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.
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.
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.
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.
Recommended by LinkedIn
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.
Explicitly extending Repository interface. But this gives you less methods.
Today, Spring recommends extending from JpaReposity as it provides rich functionality to access data.
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.
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.
Certified Oracle Cloud Infrastructure Architect Professional
2yInformative👌🏽
Android | Kotlin | Java| Digital Transformations| DevSecOps|Writer
2yInteresting read.