Spring Data JPA is a framework, originally developed by a Spring Pivotal team and runs top of the hibernate. In another word Spring data JPA is an implementation of Java Persistence API. Makes it easy to easily implement JPA based repositories.
JPA
JPA is a short term of Java Persistence API. It is just a specification that facilitates object relational mapping to manage relational data in Java applications, It is one of the advance feature in Java. It helps to convert the Java POJO object into table data and table data into Java POJO objects. And do not need to write a SQL statement. JPA will take care of SQL query generation based on entity class that we are working on. But we can not directly use the JPA because its just a specification, so we need ORM framework.
ORM Framework
ORM is short term of Object Relational Mapping. And it is a functionality which is used to develop and maintain the relationship between an object and relational database and relational database to object. There are many ORM frameworks in the market. Those all implementations of JPA.
- Hibernate
- Spring Data JPA
- TopLink
- ORMLite
- iBATIS
- JPOX
Spring Data JPA Features
- Sophisticated support to build repositories based on Spring and JPA
- Support for Querydsl predicates and thus type-safe JPA queries
- Transparent auditing of domain class
- Pagination support, dynamic query execution, ability to integrate custom data access code
- Validation of
@Query
annotated queries at bootstrap time - Support for XML based entity mapping
- JavaConfig based repository configuration by introducing
@EnableJpaRepositories
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/samplecoder
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
spring.jackson.serialization.order-map-entries-by-keys=true
spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false
Entity Class
package com.samplecoder.emo.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private int std;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getStd() {
return std;
}
public void setStd(int std) {
this.std = std;
}
}
Repository class
package com.samplecoder.emo.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.samplecoder.emo.entity.Student;
@Repository
public interface StudentRepository extends JpaRepository<Student, Integer>{
}
Rest Controller
package com.samplecoder.emo.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.samplecoder.emo.entity.Student;
import com.samplecoder.emo.repository.StudentRepository;
@RestController
@RequestMapping("/api/student")
public class StudentController {
@Autowired
private StudentRepository studentRepository;
@PostMapping("/save")
private Student save(@RequestBody Student student) {
student = studentRepository.save(student);
return student;
}
@DeleteMapping("/delete/{id}")
private ResponseEntity<String> delete(@PathVariable("id") Integer id){
studentRepository.deleteById(id);
return new ResponseEntity<String>("DELTETED", HttpStatus.OK);
}
@GetMapping("/get/{id}")
private Student get(@PathVariable(name = "id", required = true) Integer id) {
return studentRepository.getById(id);
}
@GetMapping("/list")
private List<Student> list(){
return studentRepository.findAll();
}
}
Test Result
//Create
Request: http://localhost:8080/api/student/save
Body:
{
"name":"Muthu",
"std":10
}
Response:
{
"id":1,
"name":"Muthu",
"std":10
}
//Update
Request: http://localhost:8080/api/student/save
Body:
{
"id":1,
"name":"Muthu",
"std":12
}
Response:
{
"id":1,
"name":"Muthu",
"std":12
}
//Get By ID
Request: http://localhost:8080/api/student/get/1
Response:
{
"id": 1,
"name": "Muthu",
"std": 12,
"hibernateLazyInitializer": {}
}
//List All
Request: http://localhost:8080/api/student/list
Response:
[
{
"id": 1,
"name": "Muthu",
"std": 12
}
]
//Delete
Request: http://localhost:8080/api/student/delete/1
Response: DELTETED
One Comment on “Spring Data JPA”
Comments are closed.