Professional Documents
Culture Documents
www.luv2code.com
Various DAO Techniques
www.luv2code.com
Various DAO Techniques
www.luv2code.com
Various DAO Techniques
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
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
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
// 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
// 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
// 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
// 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
// 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:
www.luv2code.com
My Wish
• I wish we could tell Spring:
www.luv2code.com
My Wish
• I wish we could tell Spring:
www.luv2code.com
My Wish Diagram
www.luv2code.com
My Wish Diagram
www.luv2code.com
My Wish Diagram
www.luv2code.com
My Wish Diagram
www.luv2code.com
My Wish Diagram
www.luv2code.com
My Wish Diagram
Entity: Employee
www.luv2code.com
My Wish Diagram
www.luv2code.com
My Wish Diagram
Entity: Employee
Customer Primary key: Integer
www.luv2code.com
My Wish Diagram
Entity:
Entity: Employee
Customer
Product Primary key: Integer
www.luv2code.com
My Wish Diagram
Entity:
Entity: Employee
Customer
Product Primary key: Integer
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!!
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
JpaRepository
www.luv2code.com
JpaRepository
• Spring Data JPA provides the interface: JpaRepository
www.luv2code.com
JpaRepository
• Spring Data JPA provides the interface: JpaRepository
www.luv2code.com
JpaRepository
• Spring Data JPA provides the interface: JpaRepository
findAll()
www.luv2code.com
JpaRepository
• Spring Data JPA provides the interface: JpaRepository
findAll()
findById(…)
www.luv2code.com
JpaRepository
• Spring Data JPA provides the interface: JpaRepository
findAll()
findById(…)
save(…)
www.luv2code.com
JpaRepository
• Spring Data JPA provides the interface: JpaRepository
findAll()
findById(…)
save(…)
deleteById(…)
www.luv2code.com
JpaRepository
• Spring Data JPA provides the interface: JpaRepository
findAll()
findById(…)
save(…)
deleteById(…)
… others …
www.luv2code.com
JpaRepository
• Spring Data JPA provides the interface: JpaRepository
Entity: Employee
findAll()
findById(…)
save(…)
deleteById(…)
… others …
www.luv2code.com
JpaRepository
• Spring Data JPA provides the interface: JpaRepository
findAll()
findById(…)
save(…)
deleteById(…)
… others …
www.luv2code.com
Development Process Step-
By-S
tep
www.luv2code.com
Development Process Step-
By-S
tep
www.luv2code.com
Development Process Step-
By-S
tep
www.luv2code.com
Development Process Step-
By-S
tep
No need for
implementation class
www.luv2code.com
Step 1: Extend JpaRepository interface
www.luv2code.com
Step 1: Extend JpaRepository interface
www.luv2code.com
Step 1: Extend JpaRepository interface
Entity type
www.luv2code.com
Step 1: Extend JpaRepository interface
Primary key
Entity type
www.luv2code.com
Step 1: Extend JpaRepository interface
Primary key
Entity type
www.luv2code.com
Step 1: Extend JpaRepository interface
Primary key
Entity type
findAll()
findById(…)
save(…)
deleteById(…)
… others …
www.luv2code.com
Step 1: Extend JpaRepository interface
Primary key
Entity type
www.luv2code.com
Step 1: Extend JpaRepository interface
Primary key
Entity type
www.luv2code.com
JpaRepository Docs
www.luv2code.com
JpaRepository Docs
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
www.luv2code.com
Advanced Features
• Advanced features available for
www.luv2code.com
Advanced Features
• Advanced features available for
www.luv2code.com
Advanced Features
• Advanced features available for
www.luv2code.com/spring-data-jpa-defining-custom-queries
www.luv2code.com