What is JPA
JPA stands for Java Persistence API. The Java Persistence API is a persistence mechanism which can be used in Java SE and Spring applications.
- It is designed to simplify the persistence of POJOs (Plain Old Java Objects), called Entities in relational databases.
entities¶
Instances of Java objects. These classes are configured to represent a table from a relational database, and store a row of data.
-
It employs object-relational mapping (ORM) to accomplish this.
-
ORM translates SQL tables and columns to Java classes and fields (Entities). Prior to JPA we had to do this manually.
JPA is mostly made up of interfaces, and each of these interfaces is implemented by yet another dependency known as a persistence provider.
#### persistence provider #### The implementation of the JPA interfaces.
- We will be using the Hibernate persistence provider with JPA. Hibernate will actually manage the connection to the SQL database, JPA will handle the translation from SQL -> Java and back.
The creation and persistence of Entities is managed by an EntityManager. The EntityManager is just an object representing the persistence provider's connection to the database.
Once configured, the EntityManager will provide methods that make CRUD operations simple. All we have to worry about is writing the Java code; the EntityManager will translate these methods to the corresponding SQL to complete your operation.
Image courtesy of Hibernate Users Guide
The classes, interfaces, and annotations that we will use to interact with our data are predominantly located in the javax.persistence package.
Drill¶
You are going to create a new JPA Project and add to it over the next several sections, eventually creating an Entity that you can test with an EntityManager.
- Before we proceed with the hands on, we are going to download a plugin for STS. Workspaces are great for organizing code and reducing clutter, but it's annoying to have to switch back and forth between workspaces when you want to look at old code. Download the
macOS Eclipse Launcherplugin from the Eclipse marketplace. This gives you the ability to have multiple STS windows open at once.-
In a terminal, navigate to your SD/Java directory and make a new directory named VideoStore. In STS, select File -> Open Workspace -> Other -> Browse. This will open Finder, select the VideoStore folder.
-
This might be a good time to configure workspace-specific settings, such as:
- Check the Link with editor button in Package Explorer.
- In STS Preferences:
- Java | Compiler | Compliance level
- Gradle | Version
- Run/Debug | Console | Uncheck Limit console output
- General | Workspace | Check Show full workspace path (this will be helpful when we have multiple instances of Eclipse open in different workspaces)
- General | Web Browser | Use external web browser
-
Create a new JPA Project. Be sure you do not do a new Java or Dynamic Web Project like we have in the past.
-
File|New|Other -
Type
jpain the filter field and chooseJPA Project -
Click
Next.
-
-
On the JPA Project page, name the project JPAVideoStore, click Next.
-
On the Java page, add three new source folders so our project structure follows current Gradle conventions:
src/main/resourcessrc/test/javasrc/test/resources
Click Next
-
On the JPA Facet page for JPA Implementation, choose Disable Library Configuration. Click Finish
-
If prompted to open the JPA perspective, choose Remember my decision and click No.
-
Don't worry if there appears to be a project error, a later step will clean that up.
-
Open
src/main/javaand drag theMETA-INF/folder down tosrc/main/resources. -
Right-click on the project name and go all the way down to Properties.
-
In Project Facets, make sure the Java version is set to
1.8, then click Apply and Close. -
Right-click on the project name and under Configure, choose Add Gradle Nature.
-
Under Window | Show View | Other, find and open the Gradle Tasks view.
-
In the Gradle Tasks view, open the project, and open the
build setupgroup. - Double-click the
build inittask - this will initialize the Gradle files for this project. In Gradle Executions it will appear to be running something. Actually, it's waiting for some necessary input from us. -
Go to the Console view where you'll see a series of prompts for options for this project. Hit Return to accept the default for each question until you see
BUILD SUCCESSFUL. -
Right-click on the project and do a Refresh, then a Gradle | Refresh Gradle Project. You should now see the gradle files.
-