Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Spring Boot - Spring Data JPA


To understand Spring Data JPA, we need to have a little bit of understanding on JPA and ORM.


JPA stands for Java Persistence API


And


ORM stands for Object Relational Mapping.


To understand JPA and ORM, let us go back to the example we have used in the previous tutorials.


Say, you are the Principal in a reputed School and the School Inspector has visited your school and has come up with a lot of queries.


Now the school Inspector came up with a new query. i.e. All the student details must be saved in a database.


And what you have done is, in the database, created a table named Student with the fields Name, Roll, Age, and ClassName.



Name Roll Age ClassName

Now, in order to save the student details to the database, you need to perform the following steps in your Spring Boot code,

  1. Register the Driver Class.


  2. Create a Connection Object.


  3. Create the statement object.


  4. Execute the Query.


  5. Close the Connection Object.


However, with Spring Data JPA, you don't have to write any of the above steps.


And why is that?


Let's have a sneak peek at our code.


To save the student details to the database, we must have a class named Student with the attributes, Name, Roll, Age, and ClassName.


public class Student {
	String name;
	int roll;
	int age;
	int className;

	// Getters and Setters
}

So, the class Student looks like,



name roll age className

Now, don't you think the above class exactly looks like the Student table,



Name Roll Age ClassName

And yes! The Student class exactly looks like the Student table in the database.


And this is precisely where ORM comes in picture.


ORM - Object Relational Mapping


ORM which stands for Object Relational Mapping, maps the Student class with the table student because both have the same structure.

Spring Boot - Spring Data JPA

And when you load the Student object with values, ORM picks those values and loads it to the database.


Hibernate is one of the widely used ORM today.


Note : If you are having a tough time understanding the above lines. Don't worry, we will make it easy with the examples.

Now, that you got some understanding of ORM. Let us understand, what is JPA?


JPA - Java Persistence API


As said earlier, Hibernate is one of the widely used ORM. But let's say, you want to switch to some other ORM at a later point of time.


How would you do that?


Ideally, you have to change all the Hibernate related configuration in your spring boot classes. Which means a lot of refactoring of code.


And to save you from this pain, JPA or Java Persistence API comes in picture.


How does JPA does that?


In your spring boot classes where you were supposed to use Hibernate(i.e. The ORM) related stuff, simply DO NOT use that.


Use JPA related stuff in all those classes. And JPA internally connects with Hibernate(i.e. The ORM) and everything remains the same.


Now, how does JPA connects to hibernate?


There is one configuration file(We will be seeing that later) in Spring Boot, where you have to specify that the ORM you need to use is Hibernate.


Now, if you want to use some ORM other than Hibernate, all you need to do is, change in the configuration file only and you don't have to make any change in the Spring Boot classes.


Now that we have understood ORM and JPA, let us understand Spring Data JPA.


Spring Data JPA


Spring Data JPA takes the concept of JPA one step ahead.


As we know, when you connect to database, there are some common operations like :

  1. Get all data from a table in a database. Say we want to get all the students in the student

    table.

    Name Roll Age ClassName
    John 1 8 5
    Paul 2 7 5
    Andrew 3 8 5
    Kevin 4 9 5

  2. Say, we want to get the details of the student Kevin,



    Name Roll Age ClassName
    Kevin 4 9 5

  3. Update the name of Kevin to Kevin Killian



    Name Roll Age ClassName
    Kevin Killian 4 9 5

And there are many more actions that are common. Spring Data JPA makes an intelligent guess, so that you don't have to write those actions again.


Spring Data JPA provides the above actions(Like get student details or get details for a specific student) in the form of methods, which you can simply call and get your work done.


In a nutshell, Spring Data JPA provides inbuilt methods, which lets you perform common actions in the database from your code.


In the next tutorials, we will be seeing practical usage of Spring Data JPA.