Creating a Spring Boot REST API with iPad Pro and Raspberry Pi 4

Setup Spring Boot JPA Entities Part 1

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

Create the JPA entities in order to represent the data that will be persisted in the database.

Keywords

  • iPad Pro
  • Raspberry Pi
  • Code Server
  • Java
  • Spring Boot
  • PostgreSQL
  • JPA

About this video

Author(s)
Andrei Visan
First online
19 January 2022
DOI
https://doi.org/10.1007/978-1-4842-8060-7_4
Online ISBN
978-1-4842-8060-7
Publisher
Apress
Copyright information
© Andrei Visan 2022

Video Transcript

Hi, all, and welcome to Setup Spring Boot JPA Entities Part 1. Now let’s go ahead and create our Database Entity classes. So I’m going to create a package here under My Finances, and I’m going to call it Entity.

And let’s create the first class. So what I want? I want to categorize my expenses. So I’m going to create a Category class. And here, it’s exactly why I said that we need to use Lombok because by using Lombok, we don’t need to create getters and setters.

So I’m just going to Say getter, which comes from Lombok. I’m just going to say Setter. I’m just going to say also NoArgsConstructor. We need to import all this. Cool.

Now that we have that, we also need to tell Spring that this is a database Entity so that Spring will know to save it into the database. And for that, you have to use the javax.persistence package.

And also another thing I want to do is also I want to put it in a table name, and I want to set up that table name myself. So I’m just going to put here Table name equals. In here I’m just going to say “category.”

And now we don’t need to create getters and setters, we just need to create the fields themselves. So for the category, I just want to have two fields. I want to have an ID, which will be auto-generated, and I want to have a label for my category. So I’m going to say here private Long id and private String label.

Now I’m going to tell Spring that this is the ID so that it will be auto-generated and it comes with all the properties of an ID field. I also want to create a sequence generator. And what will that do? That will automatically increase the ID once we add categories. So this is why I want to use the sequence generator because I want to have that automatically, and I want it set up in a sequence.

So I’m going to put the name of the sequence here. And then we’ll call it category_sequence. We also have to use here sequenceName, and then it is just the same. Enter the allocationSize, this meaning all the time increase by one in this case.

Now I also want to tell Spring that this is an auto-generated value. So I’m going to say GeneratedValue, strategy. So since I created the sequence, I want the strategy, in this case, to be a sequence.

I don’t Spring to automatically create the IDs. I wanted to use the sequence that I just created. So for strategy, I’m going to say GenerationType.SEQUENCE. And I also want to define the generator. So I’m going to say generator equals category_sequence.

And also let’s also call the Column annotation because I want to define here that the name should be id. And I want to say that updatable is false. So when someone saves an entity, I don’t want the client to update the ID of the category. I want it to be done using the sequence just as we defined above.

Now for the label, I’m just going to use the Column annotation, and I’m going to say name will be label. Probably by default, it would have been label anyway because it takes the name that I declare here, but I prefer to set them myself. I feel like I’m more in control of it.

I don’t want this to be nullable, meaning that this field will not accept a null value. I want it to be unique because I don’t want to have more than one category with the same name. And the columnDefinition will be TEXT. And that’s it for the Category class.

With the same structure that I have built here, I also want to build a payment system. Payment system will also be part of our Transaction entity or Expense entity, how I called it. And this will be, which payment system did you use to pay or to execute that specific transaction?

So let’s create a new class. And here I’m just going to do as above. I’m going to say Getter, I’m going to say Setter, and NoArgsConstructor. I’ll also tell Spring that this is an entity.

And also the table, I want it to be called– in this case, I have to define it myself, payment_system. Now I also want to say here that this will have an ID and also a label. So just as we did for the Category class, we will define this column as a id.

Then we’ll also create a sequence generator for this. And here, just like before, I’m going to set up a name and a sequenceName, and also an allocationSize of one. For the GeneratedValue, I’m going to say, just like before, the strategy I want it to be sequence. And I also want that my generator should be payment_system_sequence.

And let’s define also the Column annotation. In here, I’m going to say name will be id. And just like before, I don’t want it to be updated. And that’s it for the ID.

And then let’s also define here a Column annotation, which will take– of course, the name will be label. I don’t want it to be nullable, I want it to be unique, and I want the columnDefinition to be TEXT.

One thing I forgot is to create here a constructor because besides the no arguments constructor, that will be automatically created by Lombok. I also want to have a constructor which will create my category solely based on label. Let’s remove this class from here, it’s not needed.

And I’m going to code the same constructor also for the payment system. So I’m going to say here public PaymentSystem. We’ll take the label and say this label equals Label.