We need a domain model object to map the records that are 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 it, and name it model. The fully qualified package name will be com.packtpub.thorntail.footballmanagermicroservice.model.
- After that, we will build the domain class, named FootballManager (only the fields section is shown here):
...
/**
* Domain model class that maps the data stored into football_manager table
* inside database.
*
* @author Mauro Vocale
* @version 1.0.0 19/08/2018
*/
@Entity
@Table(name = "FOOTBALL_MANAGER")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "FootballManager.findAll", query
= "SELECT f FROM FootballManager f")
})
public class FootballManager {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "ID")
private Long 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 = "NICKNAME")
private String nickname;
...
}
- To complete the management of database access operations, we need to configure the persistence.xml file, inside the src/main/resources/META-INF directory, where we will store all of the configurations related to our persistence layer. Remember to set:
- The name of the persistence unit (FootballManagerPU)
- 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/FootballManagerDS)
- The load script source that will preload a subset of data into the database
- Finally, we will create the default project (the defaults.yml file) in the src/main/resources/ directory, where we will define the data source, FootballManagerDS, that contains the configuration to connect to the database:
swarm:
datasources:
data-sources:
FootballManagerDS:
driver-name: mysql
connection-url: jdbc:mysql://localhost:3306/football_managers_registry
user-name: root
password: root
valid-connection-checker-class-name: org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker
validate-on-match: true
background-validation: false
exception-sorter-class-name: org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter
- Let's complete the process by setting the relative dependency to io.thorntail.datasources and the mysql driver in the Maven pom.xml file.
- Declaring the vendor JDBC library dependency, Thorntail will automatically detect and install the driver.