Object-relational Mapping Using JPA, Hibernate and Spring Data JPA. Persistence with JPA


JPA application

The persistence unit

  • As JPA is only a specification, we need to indicate the vendor-specific PersistenceProvider implementation of the API. The persistence we define will be backed by a Hibernate provider. #1
  • We indicate the JDBC properties — driver #2, URL of the database #3, username #4, and password #5 to access it. The machine we are running the programs on has MySQL 8 installed and the access credentials are the ones from persistence.xml.
  • The Hibernate dialect is MySQL8 #6, as the database to interact with is MySQL Release 8.0.
  • Every time the program is executed, the database will be created from scratch #7. This is ideal for automated testing when we want to work with a clean database for every test run.

The persistent class

  • Persistent classes need the @Entity annotation #1, to indicate they correspond to a database table. If no table name is specified, this class will be mapped by default to a table called ITEM.
  • Persistent classes need an attribute annotated with @Id #2, to correspond to the primary key of the database table. This attribute will be mapped to a column named ID.
  • The @GeneratedValue annotation indicates the automatic generation of primary keys #3. Many strategies may be used here, depending on the database: identity fields, values obtained from a sequence, or a table.
  • Attributes will be mapped to columns in the table. As no column name is specified, info attribute #4 will be mapped by default to a column called INFO.
  • We need an EntityManagerFactory to access the database #1. This factory corresponds to the persistence unit that we have previously defined.
  • Create an EntityManager and start a new database session #2.
  • Get access to the standard transaction API and begin a transaction #3.
  • Create a new instance of the Item class, and set its info property #4.
  • The transient instance becomes persistent on the side of the persistence context #5.
  • Commit the transaction #6. A new row is inserted in the ITEM table. Access to the database must be transactional, even if it is read-only.
  • As we created an EntityManager, we must close it #7.
  • As we created an EntityManagerFactory, we must close it #8.
  • There is no SQL code and no JDBC usage in the JPA application. There are no CRUD (create, read, update, delete) operations inside the Java code, but only working object-oriented way, with classes, objects, and methods. The translation to each SQL dialect is made by the ORM which also addresses portability.


Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

🐳 Deploy Docker 1.13 Swarm mode Stack

How to show SQL Count beside Navigation Menu in Oracle APEX

Writeup Reverse Engineering BSides Delhi CTF 2020

Planning For Technology Continuity In Your Startup

Project Unicorn: The Third Ideal — Improvement of Daily Work

Installing Kali Linux in Virtual Box

Google has terminated my start-up Google Play Publisher Account And an Open challenge

How to Fix UnicodeEncodeError: 'ascii' codec can't encode character in Python and Pandas

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Luxoft Training

Luxoft Training


More from Medium

Create a spring boot application that stores images

Spring Profiling

Tree Entity with JPA and spring-boot

Spring Cloud Config| Configuring Random Values in properties.