Spring JDBC and JPA

JDBC (short for Java Database Connectivity). The JDBC API defines how we can access the data stored in a relational database. A JDBC driver is the implementation of the JDBC API for a specific type of database. For example, com.mysql.jdbc.Driver is the driver class name for the MySQL database and org.hsqldb.jdbcDriver is the driver class name for HSQLDB, a relational database written in pure Java. Spring JDBC is an abstraction layer provided by Spring above the JDBC API to make it easier for us to interact with the database.

JPA (short for Java Persistence API). It defines a Java standard approach for Java objects persistence, using an object-relational mapping (ORM) mechanism to bridge the gap between an object-oriented model and the data stored a relational database. Hibernate ORM is the most popular implementation of JPA standard. Besides the standard APIs, Hibernate ORM also has its native APIs. In this book, we will use Hibernate ORM as our persistence solution, using the word Hibernate for Hibernate ORM.

JDBC and JPA are two sets of APIs for solving different issues. JDBC API solves interaction with the database, while JPA solves storing/retrieving of objects to/from databases in an object-oriented way. Underneath, JPA implementations rely on JDBC drivers to access databases.

In this section, we will implement the messages persistence feature of our Messages App adopting three different approaches—using JDBC driver directly, using Spring JDBC, and using Hibernate. In this way, you can see how the lower-level implementation works, as well as the benefits of using Spring JDBC and Hibernate. We will use MySQL Community Server as our database.

We will store messages in the messages table in the database. This table has three columns—id, text, and created_date. And we have a Message class in Java code, which has three fields—id, text, and createdDateWe will add an API to MessageController for saving new messages using the HTTP POST method. The new message will use JSON format.

The following shows the object/relational mapping between the objects live inside Java Virtual Machine (JVM) and the data records in the database:

Figure 3.4: Object/relational database mapping

Before we start, if you don't have MySQL installed, you will need to download and install MySQL at https://dev.mysql.com/downloads/mysql/. At the time of writing, the latest version is 5.7. All of the code in this book will work with MySQL Version 5.6 and above. Once MySQL is ready, create a database named app_messages. After that, use the following SQL to generate the messages table:

DROP TABLE IF EXISTS `messages`;
CREATE TABLE `messages` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`text` varchar(128) COLLATE utf8_bin NOT NULL DEFAULT '',
`created_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
..................Content has been hidden....................

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