How Spring Data JPA provide Data Access Interface

2019-03-04 06:53发布

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?

1条回答
虎瘦雄心在
2楼-- · 2019-03-04 07:39

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 the SimpleJpaRepository 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.

查看更多
登录 后发表回答