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, as follows:

  1. Let's create a new Java package named model, in order to store the data model class. The fully qualified package name will be com.packtpub.thorntail.footballplayermicroservice.model.
  2. Next, we will build the domain class, named FootballPlayer:
package   com.packtpub.thorntail.footballplayermicroservice.model;

import java.math.BigInteger;
...

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

@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;
}

...

@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}

@Override
public boolean equals(Object object) {
if (!(object instanceof FootballPlayer)) {
return false;
}
FootballPlayer other = (FootballPlayer) object;
return !((this.id == null && other.getId() != null)

|| (this.id != null && !this.id.equals(other.getId())));
}

...

}
  1. To complete the management of the database access operations, we need to configure the persistence.xml file, inside  src/main/resources/META-INF directory, where we will store all the configurations related to our persistence layer:
<?xml   version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="FootballPlayerPU" transaction-type="JTA">
<jta-data-source>java:/jboss/datasources/FootballPlayerDS</jta-data-source>
<properties>
<property name="javax.persistence.schema-generation.database.action"
value="drop-and-create"/>
<property name="javax.persistence.schema-generation.drop-source"
value="metadata"/>
<property name="javax.persistence.schema-generation.create-script
source"
value="META-INF/create.sql" />
<property name="javax.persistence.sql-load-script-source"
value="META-INF/load.sql"/>
</properties>
</persistence-unit>
</persistence>

The most important elements are as follows:

  • The name of the persistence unit (FootballPlayerPU)
  • The transaction type, JTA (which means that we demand to the container the management of the transactions)
  • The data source JNDI name to use (java:/jboss/datasources/FootballPlayerDS)
  • The load script source that will preload a subset of data into the database
  1. Finally, we will create the default project (the defaults.yml file) in the src/main/resources/ directory, where we will define the data source, FootballPlayerDS, which contains the configuration to connect to the database:
swarm:
datasources:
data-sources:
FootballPlayerDS:
driver-name: postgresql
connection-url:jdbc:postgresql://localhost:5532/football_players_registry
user-name: postgres
password: postgresPwd
valid-connection-checker-class-name: org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLValidConnectionChecker
validate-on-match: true
background-validation: false
exception-sorter-class-name: org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLExceptionSorter
  1. We will set the relative dependency in a Maven pom.xml file, as follows:
<properties>
...
<version.postgresql>9.4.1207</version.postgresql>
</properties>
<dependency>
<groupId>io.thorntail</groupId>
<artifactId>datasources</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${version.postgresql}</version>
</dependency>

Declaring the vendor JDBC library dependency, Thorntail will automatically detect and install the driver.

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

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