Entity class – JPA

We need a domain model object to map the records inserted into our database. To do this, we will use the JPA specification; so, we will create an entity class for this purpose.

Let's create a new Java package to store the domain model object, and name it model. The fully qualified package name will be com.packtpub.springboot.footballplayermicroservice.model.

Next, we will build the domain class, named FootballPlayer:

...

import java.io.Serializable;
import java.math.BigInteger;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

/**
* Domain model class that maps the data stored into football_player table
* inside database.
*
* @author Mauro Vocale
* @version 1.0.0 29/09/2018
*/
@Entity
@Table(name = "football_player")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "FootballPlayer.findAll", query
= "SELECT f FROM FootballPlayer f")
})
public class FootballPlayer implements Serializable {

private static final long serialVersionUID = -92346781936044228L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;

@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50)
@Column(name = "name")
private String name;

@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50)
@Column(name = "surname")
private String surname;

@Basic(optional = false)
@NotNull
@Column(name = "age")
private int age;

@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50)
@Column(name = "team")
private String team;

@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50)
@Column(name = "position")
private String position;

@Column(name = "price")
private BigInteger price;

public FootballPlayer() {
}

public FootballPlayer(String name, String surname, int age,
String team, String position, BigInteger price) {
this.name = name;
this.surname = surname;
this.age = age;
this.team = team;
this.position = position;
this.price = price;
}

...

As you can see, this code is same as the code created in Chapter 4, Building Microservices Using Thorntail. This means that you can use JPA specifications not only in Java EE/Jakarta EE projects, but also with the Spring framework. The specifications are always portable.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset