How Spring Data JPA provide Data Access Interface instead of hibernate Data Access Layer

别等时光非礼了梦想. 提交于 2019-12-02 17:52:45

问题


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:


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.



来源:https://stackoverflow.com/questions/46541893/how-spring-data-jpa-provide-data-access-interface-instead-of-hibernate-data-acce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!