Hi dev! Are you looking for an introduction to JPA? If yes, welcome to for-devs.com!
In this introductory article to JPA or Java Persistence API. We will briefly discuss its definition, some of its utilities and a simple implementation example. Go for it.
Introduction: What is JPA?
Short definition
The short definition is that JPA (Java Persistence API) is a Java specification that provides us with ways or methods to interact with databases through an application written in Java.
Long Definition
Now, a more extensive definition would be to say that JPA is a mechanism through which an application made in Java can retrieve information from a non-volatile storage system. That is, it recovers data from a computer memory that stores information even without requiring power and keeping it intact.
Likewise, JPA is a persistence framework that abstracts us from databases. It provides a standard that saves a lot of work and reduces complex processes when it comes to persisting information. In addition, it runs on the Java platform in its Standard (Java SE) and Enterprise (Java EE) editions.
This persistence API is the Java standard for mapping (assigning) objects to a relational database.
How does the JPA API work?
Something to keep in mind is that JPA defines only specifications. That is, it only tells us what has to be done. Rather, JPA is provided as a standard reference implementation by ORM providers. Some providers, such as Hibernate, EclipseLink, and OpenJPA, also make data persistence possible by implementing JPA.
Object Relational Mapping Flow
The mapping of Java objects to database tables and vice versa is called object-relational mapping. In English it is known by its acronym as ORM.
The objective pursued by the design of this API is not to lose the advantages of object orientation when interacting with a database. It tries to faithfully follow the object-relational mapping pattern. Thus, it allows you to use regular objects known as POJOs. Let’s see the following image:
Java object models are Plain Old Java Object (POJO) classes that are exchanged in the persistence layer of the ORM provider of our choice. This will map the objects with the database tables that we indicate.
The following diagram shows the mapping between the model that represents a car object in Java and the car’s relational table in the database:
Hibernate is one of the ORM frameworks that implements the JPA specification. It is one of the most popular used in enterprise Java applications.
What are the advantages of using JPA?
Although there are innumerable advantages of using the framework for the development of our Java applications, in this introductory article to JPA I will only mention a few:
- The system load for interacting with the database is significantly reduced by using JPA.
- It is the standard for object-relational mapping and data persistence. JPA provides the standard API.
- User programming becomes easy by hiding direct mapping from a Java model.
- The cost of creating the definition (xml) file is reduced by extensive use of annotations.
- We can merge the applications used with other JPA providers.
- It is based on Plain Old Java Object (POJO).
Example
Below, in this section, we’ll look at a basic example of how to use JPA to store and retrieve table data from a database:
Remember that JPA works heavily with annotations. In this way, let’s imagine that we want to map a JAVA class to a table in a database. It is so simple that you just have to annotate it with @Entity
and also add the @Id
annotation on one of the attributes that serves as a key or primary key.
For example, the following piece of code could be a class annotated with @Entity
that later allows us to store, retrieve, or update fields on a “user” table.
@Entity // tells JPA that this class is an Entity in the DB public class User { @id // Tells JPA which attribute of the JAVA class should be considered the unique key. private String id; private String name; private String email; }
For now, I’ll just show you a short example of using JPA annotations to map the Java class to the table in the database. With the above, we still could not perform operations on the database. To do this, it would be necessary to create a Repository class to interact with the table in the database and use that Repository in a Service class that helps us store and retrieve data.
Ok, without going into definitions, since using JPA annotations is pretty intuitive, let’s look at some very common ones:
- @Entity
- @id
- @Embeddable
- @OneToOne
- @OneToMany
- @ManyToOne
- @ManyToMany
In case you want to go deeper into the subject, you can visit the following links to the official JPA documentation on the oracle.com page:
- 37 Introduction to the Java Persistence API (Release 7) (oracle.com)
- javax.persistence (Java (TM) EE 7 Specification APIs) (oracle.com)
Conclusion
Undoubtedly, JPA together with an ORM allows us to interact with databases in a simple way and, in addition, it is quick to implement.
The annotations save several lines of code and the framework itself offers efficient solutions for complex processes happening in the background. Consequently, JPA is positioned as the implementation standard that provides solutions to processes in the data persistence layer.
Therefore, this article briefly and concisely addresses the definition of some key concepts, highlighted features and a small example that serves as an introduction to JPA, the Java data persistence framework.
Nothing to add. Thank you for visiting for-devs.com!