Spring Data JPA in Spring Boot
Various DAO Techniques
www.luv2code.com
Various DAO Techniques
• Version 1: Use EntityManager but leverage native Hibernate API
www.luv2code.com
Various DAO Techniques
• Version 1: Use EntityManager but leverage native Hibernate API
• Version 2: Use EntityManager and standard JPA API
www.luv2code.com
Various DAO Techniques
• Version 1: Use EntityManager but leverage native Hibernate API
• Version 2: Use EntityManager and standard JPA API
• Version 3: Spring Data JPA
www.luv2code.com
Application Architecture
Employee
Employee Employee
REST
Service DAO
Controller
www.luv2code.com
Application Architecture
Employee
Employee Employee
REST
Service DAO
Controller
JPA
Previously used
JPA API
www.luv2code.com
Application Architecture
Employee
Employee Employee
REST
Service DAO
Controller
Spring Data
JPAJPA
Now use
Spring Data JPA
www.luv2code.com
The Problem
www.luv2code.com
The Problem
• We saw how to create a DAO for Employee
www.luv2code.com
The Problem
• We saw how to create a DAO for Employee
www.luv2code.com
The Problem
• We saw how to create a DAO for Employee
www.luv2code.com
The Problem
• We saw how to create a DAO for Employee
• What if we need to create a DAO for another entity?
www.luv2code.com
The Problem
• We saw how to create a DAO for Employee
• What if we need to create a DAO for another entity?
• Customer, Student, Product, Book …
www.luv2code.com
The Problem
• We saw how to create a DAO for Employee
• What if we need to create a DAO for another entity?
• Customer, Student, Product, Book …
• Do we have to repeat all of the same code again???
www.luv2code.com
Creating DAO
www.luv2code.com
Creating DAO
• You may have noticed a pattern with creating DAOs
www.luv2code.com
Creating DAO
• You may have noticed a pattern with creating DAOs
@Override
public Employee findById(int theId) {
// get data
Employee theData = entityManager.find(Employee.class, theId);
// return data
return theData;
}
www.luv2code.com
Creating DAO
• You may have noticed a pattern with creating DAOs
Most of the code
@Override is the same
public Employee findById(int theId) {
// get data
Employee theData = entityManager.find(Employee.class, theId);
// return data
return theData;
}
www.luv2code.com
Creating DAO
• You may have noticed a pattern with creating DAOs
Most of the code
@Override is the same
public Employee findById(int theId) {
// get data
Employee theData = entityManager.find(Employee.class, theId);
// return data
return theData;
}
Only difference is the
entity type and primary key
www.luv2code.com
Creating DAO
• You may have noticed a pattern with creating DAOs
Most of the code
@Override is the same
public Employee findById(int theId) {
// get data
Employee theData = entityManager.find(Employee.class, theId);
// return data
return theData;
}
Only difference is the
entity type and primary key
www.luv2code.com
Creating DAO
• You may have noticed a pattern with creating DAOs
Most of the code
@Override is the same
public Employee findById(int theId) {
// get data
Employee theData = entityManager.find(Employee.class, theId);
// return data
return theData;
}
Only difference is the Entity type
entity type and primary key
www.luv2code.com
Creating DAO
• You may have noticed a pattern with creating DAOs
Most of the code
@Override is the same
public Employee findById(int theId) {
// get data
Employee theData = entityManager.find(Employee.class, theId);
// return data
return theData;
}
Primary key
Only difference is the Entity type
entity type and primary key
www.luv2code.com
My Wish
www.luv2code.com
My Wish
• I wish we could tell Spring:
www.luv2code.com
My Wish
• I wish we could tell Spring:
Create a DAO for me
www.luv2code.com
My Wish
• I wish we could tell Spring:
Create a DAO for me
Plug in my entity type and primary key
www.luv2code.com
My Wish
• I wish we could tell Spring:
Create a DAO for me
Plug in my entity type and primary key
Give me all of the basic CRUD features for free
www.luv2code.com
My Wish Diagram
www.luv2code.com
My Wish Diagram
findAll() CRUD methods
www.luv2code.com
My Wish Diagram
findAll() CRUD methods
findById(…)
www.luv2code.com
My Wish Diagram
findAll() CRUD methods
findById(…)
save(…)
www.luv2code.com
My Wish Diagram
findAll() CRUD methods
findById(…)
save(…)
deleteById(…)
… others …
www.luv2code.com
My Wish Diagram
Entity: Employee
findAll() CRUD methods
findById(…)
save(…)
deleteById(…)
… others …
www.luv2code.com
My Wish Diagram
Entity: Employee Primary key: Integer
findAll() CRUD methods
findById(…)
save(…)
deleteById(…)
… others …
www.luv2code.com
My Wish Diagram
Entity: Employee
Customer Primary key: Integer
findAll() CRUD methods
findById(…)
save(…)
deleteById(…)
… others …
www.luv2code.com
My Wish Diagram
Entity:
Entity: Employee
Customer
Product Primary key: Integer
findAll() CRUD methods
findById(…)
save(…)
deleteById(…)
… others …
www.luv2code.com
My Wish Diagram
Entity:
Entity: Employee
Customer
Product Primary key: Integer
findAll() CRUD methods
findById(…)
save(…)
deleteById(…)
… others …
www.luv2code.com
Spring Data JPA - Solution
www.luv2code.com
Spring Data JPA - Solution
• Spring Data JPA is the solution!!!!
www.luv2code.com
Spring Data JPA - Solution
• Spring Data JPA is the solution!!!! https://spring.io/projects/spring-data-jpa
www.luv2code.com
Spring Data JPA - Solution
• Spring Data JPA is the solution!!!! https://spring.io/projects/spring-data-jpa
• Create a DAO and just plug in your entity type and primary key
www.luv2code.com
Spring Data JPA - Solution
• Spring Data JPA is the solution!!!! https://spring.io/projects/spring-data-jpa
• Create a DAO and just plug in your entity type and primary key
www.luv2code.com
Spring Data JPA - Solution
• Spring Data JPA is the solution!!!! https://spring.io/projects/spring-data-jpa
• Create a DAO and just plug in your entity type and primary key
• Spring will give you a CRUD implementation for FREE …. like MAGIC!!
www.luv2code.com
Spring Data JPA - Solution
• Spring Data JPA is the solution!!!! https://spring.io/projects/spring-data-jpa
• Create a DAO and just plug in your entity type and primary key
• Spring will give you a CRUD implementation for FREE …. like MAGIC!!
www.luv2code.com
Spring Data JPA - Solution
• Spring Data JPA is the solution!!!!
https://spring.io/projects/spring-data-jpa
• Create a DAO and just plug in your entity type and primary key
• Spring will give you a CRUD implementation for FREE …. like MAGIC!!
• Helps to minimize boiler-plate DAO code … yaaay!!!
www.luv2code.com
Spring Data JPA - Solution
• Spring Data JPA is the solution!!!!
https://spring.io/projects/spring-data-jpa
• Create a DAO and just plug in your entity type and primary key
• Spring will give you a CRUD implementation for FREE …. like MAGIC!!
• Helps to minimize boiler-plate DAO code … yaaay!!!
More than 70% reduction in code … depending on use case
www.luv2code.com
JpaRepository
www.luv2code.com
JpaRepository
• Spring Data JPA provides the interface: JpaRepository
www.luv2code.com
JpaRepository
• Spring Data JPA provides the interface: JpaRepository
• Exposes methods (some by inheritance from parents)
www.luv2code.com
JpaRepository
• Spring Data JPA provides the interface: JpaRepository
• Exposes methods (some by inheritance from parents)
findAll()
www.luv2code.com
JpaRepository
• Spring Data JPA provides the interface: JpaRepository
• Exposes methods (some by inheritance from parents)
findAll()
findById(…)
www.luv2code.com
JpaRepository
• Spring Data JPA provides the interface: JpaRepository
• Exposes methods (some by inheritance from parents)
findAll()
findById(…)
save(…)
www.luv2code.com
JpaRepository
• Spring Data JPA provides the interface: JpaRepository
• Exposes methods (some by inheritance from parents)
findAll()
findById(…)
save(…)
deleteById(…)
www.luv2code.com
JpaRepository
• Spring Data JPA provides the interface: JpaRepository
• Exposes methods (some by inheritance from parents)
findAll()
findById(…)
save(…)
deleteById(…)
… others …
www.luv2code.com
JpaRepository
• Spring Data JPA provides the interface: JpaRepository
• Exposes methods (some by inheritance from parents)
Entity: Employee
findAll()
findById(…)
save(…)
deleteById(…)
… others …
www.luv2code.com
JpaRepository
• Spring Data JPA provides the interface: JpaRepository
• Exposes methods (some by inheritance from parents)
Entity: Employee Primary key: Integer
findAll()
findById(…)
save(…)
deleteById(…)
… others …
www.luv2code.com
Development Process Step-
By-S
tep
www.luv2code.com
Development Process Step-
By-S
tep
1. Extend JpaRepository interface
www.luv2code.com
Development Process Step-
By-S
tep
1. Extend JpaRepository interface
2. Use your Repository in your app
www.luv2code.com
Development Process Step-
By-S
tep
1. Extend JpaRepository interface
2. Use your Repository in your app
No need for
implementation class
www.luv2code.com
Step 1: Extend JpaRepository interface
www.luv2code.com
Step 1: Extend JpaRepository interface
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
www.luv2code.com
Step 1: Extend JpaRepository interface
Entity type
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
www.luv2code.com
Step 1: Extend JpaRepository interface
Primary key
Entity type
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
www.luv2code.com
Step 1: Extend JpaRepository interface
Primary key
Entity type
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
// that's it ... no need to write any code LOL!
www.luv2code.com
Step 1: Extend JpaRepository interface
Primary key
Entity type
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
// that's it ... no need to write any code LOL!
Entity: Employee Primary key: Integer
findAll()
findById(…)
save(…)
deleteById(…)
… others …
www.luv2code.com
Step 1: Extend JpaRepository interface
Primary key
Entity type
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
// that's it ... no need to write any code LOL!
Entity: Employee Primary key: Integer
Get these
methods for free
findAll()
findById(…)
save(…)
deleteById(…)
… others …
www.luv2code.com
Step 1: Extend JpaRepository interface
Primary key
Entity type
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
// that's it ... no need to write any code LOL!
Entity: Employee Primary key: Integer
Get these
methods for free
findAll()
No need for findById(…)
save(…)
implementation class deleteById(…)
… others …
www.luv2code.com
JpaRepository Docs
• Full list of methods available … see JavaDoc for JpaRepository
www.luv2code.com
JpaRepository Docs
• Full list of methods available … see JavaDoc for JpaRepository
www.luv2code.com/jpa-repository-javadoc
www.luv2code.com
Step 2: Use Repository in your app
www.luv2code.com
Step 2: Use Repository in your app
@Service
public class EmployeeServiceImpl implements EmployeeService {
Our repository
private EmployeeRepository employeeRepository;
www.luv2code.com
Step 2: Use Repository in your app
@Service
public class EmployeeServiceImpl implements EmployeeService {
Our repository
private EmployeeRepository employeeRepository;
@Autowired
public EmployeeServiceImpl(EmployeeRepository theEmployeeRepository) {
employeeRepository = theEmployeeRepository;
}
www.luv2code.com
Step 2: Use Repository in your app
@Service
public class EmployeeServiceImpl implements EmployeeService {
Our repository
private EmployeeRepository employeeRepository;
@Autowired
public EmployeeServiceImpl(EmployeeRepository theEmployeeRepository) {
employeeRepository = theEmployeeRepository;
}
www.luv2code.com
Step 2: Use Repository in your app
@Service
public class EmployeeServiceImpl implements EmployeeService {
Our repository
private EmployeeRepository employeeRepository;
@Autowired
public EmployeeServiceImpl(EmployeeRepository theEmployeeRepository) {
employeeRepository = theEmployeeRepository;
}
@Override
public List<Employee> findAll() {
www.luv2code.com
Step 2: Use Repository in your app
@Service
public class EmployeeServiceImpl implements EmployeeService {
Our repository
private EmployeeRepository employeeRepository;
@Autowired
public EmployeeServiceImpl(EmployeeRepository theEmployeeRepository) {
employeeRepository = theEmployeeRepository;
}
@Override
public List<Employee> findAll() {
return employeeRepository.findAll();
}
…
}
www.luv2code.com
Step 2: Use Repository in your app
@Service
public class EmployeeServiceImpl implements EmployeeService {
Our repository
private EmployeeRepository employeeRepository;
@Autowired
public EmployeeServiceImpl(EmployeeRepository theEmployeeRepository) {
employeeRepository = theEmployeeRepository;
}
@Override
public List<Employee> findAll() {
return employeeRepository.findAll();
}
…
Magic method that is
} available via repository
www.luv2code.com
Minimized Boilerplate Code
www.luv2code.com
Minimized Boilerplate Code
Before Spring Data JPA
www.luv2code.com
Minimized Boilerplate Code
Before Spring Data JPA
www.luv2code.com
Minimized Boilerplate Code
Before Spring Data JPA
www.luv2code.com
Minimized Boilerplate Code
Before Spring Data JPA
2 Files
30+ lines of code
www.luv2code.com
Minimized Boilerplate Code
Before Spring Data JPA After Spring Data JPA
2 Files
30+ lines of code
www.luv2code.com
Minimized Boilerplate Code
Before Spring Data JPA After Spring Data JPA
2 Files
30+ lines of code
www.luv2code.com
Minimized Boilerplate Code
Before Spring Data JPA After Spring Data JPA
1 File
3 lines of code!
2 Files
30+ lines of code
www.luv2code.com
Minimized Boilerplate Code
Before Spring Data JPA After Spring Data JPA
1 File
3 lines of code!
2 Files
30+ lines of code
No need for
implementation class
www.luv2code.com
Minimized Boilerplate Code
Before Spring Data JPA After Spring Data JPA
1 File
3 lines of code!
2 Files
30+ lines of code
No need for
implementation class
www.luv2code.com
Advanced Features
www.luv2code.com
Advanced Features
• Advanced features available for
www.luv2code.com
Advanced Features
• Advanced features available for
• Extending and adding custom queries with JPQL
www.luv2code.com
Advanced Features
• Advanced features available for
• Extending and adding custom queries with JPQL
• Query Domain Specific Language (Query DSL)
www.luv2code.com
Advanced Features
• Advanced features available for
• Extending and adding custom queries with JPQL
• Query Domain Specific Language (Query DSL)
• Defining custom methods (low-level coding)
www.luv2code.com
Advanced Features
• Advanced features available for
• Extending and adding custom queries with JPQL
• Query Domain Specific Language (Query DSL)
• Defining custom methods (low-level coding)
www.luv2code.com/spring-data-jpa-defining-custom-queries
www.luv2code.com