Appendix A. Introducing Spring Data JPA

The Spring Data JPA website, http://projects.spring.io/spring-data-jpa/, has an opening paragraph that succinctly describes the problems of implementing a JPA-based DAO layer:

Implementing a data access layer of an application has been cumbersome for quite a while. Too much boilerplate code has to be written to execute simple queries as well as perform pagination, and auditing. Spring Data JPA aims to significantly improve the implementation of data access layers by reducing the effort to the amount that's actually needed. As a developer you write your repository interfaces, including custom finder methods, and Spring will provide the implementation automatically.

In Chapter 4, Data Access Made Easy, we implemented the DAO design pattern to abstract database persistence into a well-defined layer. We deliberately decided not to introduce Spring Data JPA in this chapter, as the target audience were intermediate developers who may not have had experience with the Java Persistence API. JPA terminology, concepts, and practical examples were introduced to give you an understanding of how JPA works. The use of Java interfaces, Java generics, and named query concepts are fundamental to understanding the elegant way in which Spring Data JPA works.

Spring Data JPA does not require you to write an implementation of the repository interface. The implementations are created "on the fly" when you run the Spring Data JPA application. All that the developer needs to do is write the DAO Java interfaces that extend org.springframework.data.repository.CrudRepository and adhere to the Spring Data JPA naming conventions. The DAO implementation is created for you at runtime.

Internally, Spring Data JPA will implement the code that performs the same functionality that was implemented in Chapter 4, Data Access Made Easy. Using Spring Data we could, for example, rewrite the CompanyDao interface as:

package com.gieman.tttracker.dao;

import com.gieman.tttracker.domain.Company;
import java.util.List;
import org.springframework.data.repository.CrudRepository;

public interface CompanyDao extends CrudRepository<Company, Integer>{
    
}

The CompanyDao implementation will include the findAll method as it is defined in the CrudRepository interface; we do not need to define it as a separate method.

If you are comfortable with JPA and the content covered in Chapter 4, Data Access Made Easy, you should explore the Spring Data JPA framework. Implementing JPA-based repositories will then become significantly easier!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.31.165