Spring Data JPA - Part 1
Take a look at this tutorial that demonstrates how Java Object Relational Mapping works as a method of managing relational databases in Java.
Join the DZone community and get the full member experience.Join For Free
Object Relational Mapping (ORM)
A Java Object is mapped with a table in the database. The fields in the class corresponds to fields in the database.
This shows a typical representation of how the fields in the class are mapped to fields in the table.
We annotate the Class with
@Entity. We can also specify the table name. The fields in the class will correspond to the fields in the database.
Major ORM frameworks include Hibernate, Ibatis, EclipseLink and there are many more.
JPA stands for Java Persistence API. It is the standard for mapping Java Objects to a relational database. All the frameworks adhere to this specification. Here, we will use Hibernate, which conforms to JPA specification. All annotations like
@Id which we have used above forms part of the JPA framework.
When we talk of Spring data JPA, it simplifies everything for developers. The data access and CRUD operations are made very simple with Spring Data JPA. We can have Java Interfaces called repositories which extends the JPA Repository, which has many finder methods that comes with the implementation. Spring Data JPA comes with ease of implementation of paging, sorting, auditing and even supports native query operations.
Let us see a small demonstration of our first implementation.
I use the following:
- JDK 1.8
- IDE - IntelliJ
- Database - PostgreSQL
- PostgreSQL Tool - PgAdmin.
We will use the same user table as above. I will name it as
t_user. Let us see the folder structure.
- The entity package with user entity.
- The repository package with UserRepository.
The User Entity
@Entity annotation specifies the class is an entity and is mapped to the database table. Here we specify the table name with the
@Table annotation. We are giving it the name "t_user."
Note: Have fun with the table name as User. You are going to get errors. Check it out.
@Id annotation is used to mark it as the primary key and the generation type we are using it as Identity. All the values are autogenerated sequentially without using a separate sequence.
The repository extends
JpaRepository which gives us access to many finder methods and delete methods. It also has methods which return
Optional<User> which is very handy since we don't have to check for null values. They have methods like
isPresent which is very useful to check the existence of Object.
The application.properties File
|spring.datasource.username = postgres|
|spring.datasource.password = root|
- The server port is the port on which I am running the application.
- You need not change the driver name unless if you are using another database.
- show-sql will give you the SQL statements created by Hibernate printed to the console. It is good to see that for your understanding.
- I am using
ddl-auto = createsince Hibernate is going to create the tables for me automatically. Make sure you have already created the database.
My dependencies in the POM file:
You can run the application and see the user table created by Hibernate.
You can find the source code at GitHub.
You can find the video tutorial here:
Opinions expressed by DZone contributors are their own.