Spring Data JPA
Spring Data JPA provides a common set of interfaces based on Java Persistence API (JPA) leveraging method naming conventions to derive data access behaviour using JPA and Hibernate which provides aspected behaviour. Practically, this means that you simply implement an interface and allow the code to be handled by Spring through the use of aspects.
You can still make use of JDBC with Spring for queries and result set mappings but Spring will handle the connection and tear down automatically.
Spring Data also provides the repository pattern natively and includes data mapping conventions to translate result sets to entity objects.
Benefits of Spring Data
- Removes boilerplate code
- Supports swapping between multiple datasources, either embedded or remote
- Enables separation of concerns between business logic, domain entites, services, etc
Key components
Repository Interface
The repository interface provides the methods needed to access the data using an extension of a Spring class and generics that leverages proxies to build the actual base CRUD methods.
To use Spring Data JPA for a JPA entity, declare a Java interface class that extends CrudRepository which will provide the create, read, update and delete methods.
//CRUD -> Create Read Update Delete //T -> Domain type the repository manages //ID -> Type of entity ID public interface CrudRepository<T, ID>
Taking a simple example for a grocery repository:
public interface groceryRepository extends CrudRepository<Product, Integer> {
//Methods to create or update products in the groceryRepository
Product save(Product s);
Iterable<Product> saveAll(Iterable<Product> iterable);
//Methods to delete products in the groceryRepository
void deleteById(Integer integer);
void delete(Product product);
void deleteAll(Iterable<Product> iterable);
void deleteAll();
//Methods to read or lookup products in the groceryRepository
Optional<Product> findById(Integer integer);
boolean existsById(Integer integer);
Iterable<Product> findAll();
Iterable<Product> findAllById(Iterable<Integer> iterable)
long count();
}
For query methods, the method signature should be declared in accordance with Spring rules and correctly map the entity properties to the method signature. To create a simple query method:
- Declare the return type
- Begin the signature with findBy
- Entity attribute name (Use camelCase)
- Query parameters with datatype of the entity attribute
public interface groceryRepository extends CrudRepository<Product, Integer> {
List<Product> findbyProductCode(Integer id);
Optional<Product> findByName(String name);
List<Product> findByPrice(Integer price);
Collection<Product> findBySku(Sku sku);
List<Product> findBySku(Sku sku);
}
For complex query methods, where additional filters are required, refer to the Spring Data JPA documentation for all the filter combinations
public interface groceryRepository extends CrudRepository<Product, Integer> {
List<Product> findByProductPackageCodeAndLocation(String code, Location location);
List<Product> findByPriceLessThan(Integer maxPrice);
List<Product> findByNameContains(String keyword);
List<Product> findByPackageCodeAndDescriptionLike(String code, String searchString);
}
For advanced query methods, you can leverage JPQL queries inside the repository interface by using the @Query annotation.
public interface groceryRepository extends CrudRepository<Product, Integer> {
@Query("Select p from Product p where p.productPackage.code = ?1 "
+ " and p.sku = ?2 and p.location = ?3 and p.price <= ?4")
List<Product> lookupProduct(Integer code, Sku sku, Location location, Integer price);
//Same as
List<Product>findByPackageCodeAndSkuAndLocationAndPriceLessThan
(Integer code, Sku sku, Location location, Integer price);
Entity Object
The entity object is the Data Transfer Object (DTO) for the data layer which is mapped to the table of the underlying data structure using JPA. Joins are supported through entities in a Relational Database Management System (RDMS).
DataSource
Connection to datasources are configured through properties or enabled through automatic configuration with Spring Boot unless you want to interact directly with a datasource with a JDBC template.


