When When I explored hibernate JPA implementation , I got the idea of working with hibernate and creation of data access layer for fetching and inserting data into and from database. Now I am using spring + spring data JPA + Hibernate for some CRUD operation in my project.I got the idea of role of data JPA for providing the CRUD Repository for database access instead of hibernate DAO.I used my CRUD repository like this,
package com.central.repository;
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import com.central.model.*;
public interface DriverRepository extends CrudRepository<Driver, Integer> {
Driver findById(Integer id);
}
Here I am extends from data JPA's "CrudRepository" repository. When I am using this how the data JPA provides a repository for my interface? How they are inbuilt providing findAll(), save() and delete() repository method for me? Actually I have doubt in the role of how data JPA provides the repository for my interface? And how Data JPA communicate with hibernate for providing these repository implementation?.Can anyone guide to resolve the core concept of Data JPA communicating with Hibernate JPA implementation?
Spring Data creates a Proxy for the interface and for each method tries different strategies for finding/creating an implementation of the method.
The methods of
CrudRepository
are implemented in theSimpleJpaRepository
for other methods objects get created based on annotations, parameter and return types and the method name which then get invoked by the proxy.There is a wiki article about the basic concepts that might be helpful.