Simple introduction to Spring Boot - Part 3

In this part I will show how to define the Java classes to model our pets and get them stored within a relational database table.

This part assumes you have completed both Part 1 and Part 2 of this series of articles. 


We firstly need to define a Java class that will model each pet, and will therefore create a new Pet class. This class will become an entity in our database such that it will be a table within a relational database. It's good practice to use separate packages to store related items, so let's firstly create a new sub-package.

In the NetBeans Projects window under Source Packages, right-click on com.example.petsystem and select New --> Java Package...:

In the resulting New Java Package dialogue, overtype newpackage with entities so the full package path is com.exampe.petsystem.pet, and then click the Finish button: 

We now need to create a new Java class, so right-click on the com.example.petstystem.pet package and select New --> Java Class...:

In the resulting New Java Class dialogue, enter the class name as Pet but leave all the other input fields unchanged. Then click the Finish button:

Recall from Part 2 that the home page has columns for the name of the pet and its date of birth, so we just need to define these as fields. Also define a constructor and getter & setter methods as follows:

package com.example.petsystem.pet;

import java.time.LocalDate;

public class Pet {

    private String name;
    private LocalDate dateOfBirth;

    public Pet(String name, LocalDate dateOfBirth) {
        this.name = name;
        this.dateOfBirth = dateOfBirth;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public LocalDate getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(LocalDate dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }
    
}
 

You can see the above code is a standard Java class. We now need to make it ready to become a table in a relational database, so a few additions are needed.

Firstly, just above the class signature include the @Entity annotation. Click the lightbulb icon in the gutter to select the correct import which in this case is jakarta.persistence.Entity.

You should also make the class implement the Serializable interface which can be imported from java.io.

The top of the code should look as follows:

package com.example.petsystem.pet;

import jakarta.persistence.Entity;
import java.io.Serializable;
import java.time.LocalDate;

@Entity
public class Pet implements Serializable {
 
@Entity tells the Spring framework that this class should become a table in a database.
The Serializable interface informs that the objects of this class can be streamed over a network.
Each table in a relational database needs a primary key, being something that will uniquely identify each record. The most flexible approach is to create a numeric surrogate key, so add the following instance variable together with its two annotations.
    @Id
    @GeneratedValue
    private Long id; 
Both @Id and @GeneratedValue need to be imported from jakarta.persistence.

@Id informs that this field should be the primary key.

@GeneratedValue allows the database to automatically generate a unique number for each record.

The field type is Long because that allows a greater range of integer values, and is a reference type (rather than the long primitive) to allow it to be specified in the repository, which will be explained later.

Add a getter and setter method pair for the new field:

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
 

Finally, ensure there is a no-argument constructor defined. This is needed because Spring will instantiate our objects for us in certain places:

    public Pet() {
    }
 

It can be useful to override the toString() method to return a meaningful description, such as the following:

    @Override
    public String toString() {
        return "Pet{" + "id=" + id + ", name=" + name + ", dateOfBirth=" + dateOfBirth + '}';
    }
 

In Part 4 I will create the repository interface and controller class to manage the database and user interface interactions.


Print
×
Stay Informed

When you subscribe, we will send you an e-mail whenever there are new updates on the site.

Related Posts

 

Comments

No comments made yet. Be the first to submit a comment
Monday, 27 October 2025

Captcha Image