Introduction
Spring Data makes it easier to create Spring-managed applications that use new ways to access data, such as non-relational databases, map-reduce frameworks, cloud services, as well as already well-improved support for relational databases.
This article will look at one of the Spring Data sub-projects - JPA.
What can Spring Data - JPA
- Creating and maintaining repositories created using Spring and JPA
- QueryDSL and JPA query support
- Domain Class Audit
- Support batch loading, sorting, dynamic queries
- XML mapping support for entities
What you may need Spring Data - JPA
I would answer this way - if you need to quickly create a Repository layer based on JPA in the project, designed mainly for CRUD operations, and you do not want to create abstract dao interfaces for their implementation, then Spring Data - JPA is a good choice.
')
Where to begin
We assume that you already have a maven project with Spring connected, a database configured by EntityManager.
1. Add an artifact with Spring Data - JPA
<dependency>
<groupId> org.springframework.data </ ​​groupId>
<artifactId> spring-data-jpa </ artifactId>
<version> 1.0.2.RELEASE </ version>
</ dependency>
2. In your applicationContext.xml you need to add a path where your Repository interfaces will be stored.
<jpa: repositories base-package = "com.test.repository" />
3. Create an Entity and Repository interface for it.
package com.test.entity;
...
@Entity
public class Test {
@Id
@GeneratedValue (strategy = GenerationType.AUTO)
@Column (name = "id")
private Long id;
private boolean dummy;
private int tries;
...
}
package com.test.repository;
import org.springframework.data.repository.CrudRepository;
import com.test.entity.Test;
public interface TestRepository extends CrudRepository <Test, Long> {}
4. Now you can use the created interface in your application.
public class TestServiceImpl extends TestService {
@Autowired
TestRepository testRepository;
...
}
Inheriting from CrudRepository, you were able to call methods such as:
- save
- findOne
- exists
- findAll
- count
- delete
- deleteAll
without the need to implement their implementation.
Work with queries, sorting, batch loading
Consider an example: you need to make a request that selects all Test records in which the “dummy” field is set to false, and the records are sorted by the “tries” field in the order ABC.
To solve this problem, you can choose one of several options:
Option 1:
@Query ("FROM Test where dummy =? 1 ORDER BY tries ASC")
List <Product> findTests (boolean dummyVal);
or option 2:
List <Test> findByDummyOrderByTriesAsc (boolean dummyVal);
If with the first method everything is extremely simple and this is a familiar request, then the second method is to compile the name of the method, in a special way using keywords such as: “find”, “order”, the name of variables, and so on. The developers of Spring Data - JPA have tried to consider the majority of possible options that you may need.
Specification and CriteriaBuilder
If you need to write a really complex query for this you can use the Specification.
An example in which, depending on the "retries", data with different values ​​of "dummy" will be selected.
public final class TestSpecs {
public static Specification <Test> checkRetries (final int retries) {
return new Specification <Test> () {
@Override
public Predicate toPredicate (Root <Test> root, CriteriaQuery <?> query, CriteriaBuilder cb) {
if (retries> 10) {
return cb.equal (root.get ("dummy"), false);
} else {
return cb.equal (root.get ("dummy"), true);
}
}
};
}
}
The following example shows how you can use the created Specification to filter all data.
Extend your interface with the JpaSpecificationExecutor
public interface TestRepository extends CrudRepository <Test, Long>, JpaSpecificationExecutor <Test> {}
and call the findAll method passing it the created Specification
public class TestServiceImpl extends TestService {
@Autowired
TestRepository testRepository;
public void doTest () {
int retries = 5;
List <Test> result = testRepository.findAll (Specifications.where (TestSpecs.checkRetries (retries))
...
}
}
Documentation
The main project site with a list of all Spring Data subprojects .
The site of the project Spring Data - JPATechnical documentation with examples of all features.