Spring Data JPA: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 52: Line 52:
=<span id='Spring_Data_JPA_Repository'></span>Declare the JPA Repositories=
=<span id='Spring_Data_JPA_Repository'></span>Declare the JPA Repositories=


Entities are built, managed and exposed to the application by JPA repositories, which should be explicitly declared in the application. A Spring Data JPA repository is the embodiment of the Spring [[Spring_Persistence_Concepts#Repository|repository concept]] concept, which, similarly to a [[JdbcTemplate#JdbcTemplate-Based_Repository|JdbcTemplate-based repository]], conceals low-level data access details from the application while exposing Entities to the application.
Entities are built, managed and exposed to the application by '''JPA repositories'''. JPA repositories should be explicitly declared in the application. A Spring Data JPA repository is the embodiment of the Spring [[Spring_Persistence_Concepts#Repository|repository concept]] concept, which, similarly to a [[JdbcTemplate#JdbcTemplate-Based_Repository|JdbcTemplate-based repository]], conceals low-level data access details from the application while exposing Entities to the application.


<syntaxhighlight lang='java'>
<syntaxhighlight lang='java'>

Revision as of 04:46, 16 October 2018

External

Internal

Overview

Spring Data JPA is a Spring Data project that assists with implementing JPA-based repositories, which persist data in relational databases. The approach involves writing the repository interface, including custom finder methods, and Spring will provide the implementation automatically.

Spring Persistence Concepts

Spring Persistence Concepts

Spring Boot Support

To add support for Spring Data JPA to a Spring Boot project, add the following starter dependency:

dependencies {
    implementation('org.springframework.boot:spring-boot-starter-data-jpa')
}

This starter dependency also transitively includes Hibernate as the JPA implementation.

Spring Data JPA needs a database to work with. This is how to add H2 support:

dependencies {
    runtimeOnly('com.h2database:h2')
}

Adding Persistence to an Application with Spring Data JPA

Annotate Domain Objects with @Entity

Annotate your domain objects that require persistence with @Entity and designate their primary key field with @Id and optionally with @GeneratedValue. JPA requires each entity class to expose a no-argument constructor, which can be coded manually, or it can be generated with Lombok's @NoArgConstructor:

@NoArgConstructor(access=AccessLevel.PRIVATE, force=true)

Define Relationships between Entities

@ManyToMany, etc.

Declare the JPA Repositories

Entities are built, managed and exposed to the application by JPA repositories. JPA repositories should be explicitly declared in the application. A Spring Data JPA repository is the embodiment of the Spring repository concept concept, which, similarly to a JdbcTemplate-based repository, conceals low-level data access details from the application while exposing Entities to the application.

TODO

TODO

  • How to tell that a JPA repository should use a specific database. How is that configured?
  • @EnableJpaRepositories(basePackages = "com.example.dev.myproject.driver.repo")