2.2. Spring Data JPA
Spring Data JPA has the particular objective of providing convenient support for JPA-based data access layers.
This persistence framework makes JPA-based repositories easier to implement. It also reduces boilerplate for writing and executing queries and supports pagination and auditing.
We can have fully working data access functionality with only a few lines of code and without having to write a single SQL query.
With Spring Data JPA, we can declare repository methods using a naming pattern. The framework then creates the implementations for us:
public interface CampaignRepository extends CrudRepository<Campaign, Long> {
Iterable<Campaign> findByNameContaining(String name);
}
This is just a preview, as we’ll get into that later in the course.
2.3. Some Considerations
Of course, every solution has some drawbacks. In this case, we’re delegating a lot of functionality to the framework, and whenever that happens, we lose some control over what’s finally executed.
The library has to adapt to many different scenarios. In some cases, it may not offer optimal performance.
This is actually a potential issue inherent to the ORM technique used by JPA.
This isn’t a common issue. Spring Data JPA is a tested, well-designed solution with customization options that can address many problems.
2.4. Conclusions
The takeaway here is that this is a very powerful and useful solution, suitable for most projects, and definitely better than writing a lot of boilerplate manually.
It provides the tools and flexibility to address most scenarios, but it’s important to understand this doesn’t mean all scenarios.
Simply put, we don’t necessarily have to use only Spring Data JPA as our single solution across the board.
There might be edge cases, a few exceptional queries, or persistence procedures where it makes sense to use a different approach. This could mean lower-level JPA customizations or JDBC operations.
We have to be analytical to find the best tool for the job, and decide when it makes sense to mix different solutions.
To make that decision, we need to understand the persistence framework’s features and what happens behind the scenes. That’s what we’ll pursue in this course.
3. Resources
– Spring Data Overview
– Spring Data JPA Overview
– Spring Data JPA Reference