Introduction to Spring Data JPA With Inheritance in A REST Application
Indeed, Spring is a platform to makes the Java developer life more manageable. This tutorial will explore a primary feature on Spring Data JPA, such as the inheritance.
Join the DZone community and get the full member experience.Join For Free
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 serialization.
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 specialization. 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.
Opinions expressed by DZone contributors are their own.