Introduction to Spring Data JPA With Inheritance in a REST Application

Otavio Santana
xgeeks
Published in
3 min readJun 24, 2021

Indeed, Spring is a platform to make the Java developer life more manageable. This tutorial will explore a primary feature on Spring Data JPA, such as the inheritance.

Spring Data JPA makes it easy to implement JPA-based repositories quickly. This module deals with enhanced support for JPA-based data access layers, and it is a proud member of the Spring family. This tutorial will be a short intro about Spring Data JPA and its functionality.

In the second part, we could cover the basic annotations on both Spring Core and the Spring Web, focusing on Rest Controller; however, we did not create an application with a database connection.

Fortunately, it is the goal of this part to make a connection easier and clean. We’ll use Spring Data because it reduces a lot of code, and it uses the JPA annotations.

To explore the JPA annotations with a repository, we’ll create simple rest to handle a person and its tickets through plan, train, and bus. The foremost goal here is to explore the heritage of both the JPA and JSON serialisation.

It is essential to highlight that there is an impedance mismatch. Thus objects are not RDBMS, and if we don’t think about it will apply to performance and a bad design.

Only to show the heritage, there is a Ticket as an abstract class with some specialisation. We used the Ticket class as an entity with a single table strategy, and in the Jackson, the values of the subtype both are using the field type.

The repository class integrates the database smoothly because the developer does not need to handle the implementation but with an interface. This repository has several features such as query by method and uses the Query annotation to execute queries without handle the undertaking.

The last layer is the controller, where we’ll do a tight integration with the repository once it is a light application. The PersonController has the pagination features beyond the CRUD operation.

In this tutorial, we could explore the Spring Data JPA API basics using the inheritance feature on both JPA and JSON. Spring has several resources that allow us to create a Rest application with a database integration with a couple of classes. It is essential to say that it is a first step in the JPA and Spring world where there are references to go deep below.

If you enjoy working at a large scale in projects with global impact and if you like a real challenge with Java, feel free to reach out to us at xgeeks! We are growing our team, especially in Java and you might be the next one to join this group of talented people 😉

Check out our social media channels if you want to get a sneak peek of life at xgeeks! See you soon!

References:

--

--

Otavio Santana
xgeeks
Editor for

Empowering developers worldwide to deliver better software in the Cloud. Staff Eg xgeeksio