oat2023 19:33, Introduction to Spring Data JPA| Baeldung
“
Introduction to Spring Data JPA
‘SharePoint On-Prmises vs St
(Onlin: Lequel ous convient
| owe oats
Last updated October 19,2023,
|_ writen by Eugen Paraschiv(itps//wanwbaelcung.com/author/eugen)
Spring Data (https: //www.baeldung.com/category/persistence/spring-persistence/spring-data)
partner-section (https://wwwbaeldung.com/tag/partner-section) reference
‘Spring Data JPA (https:// wwwbaeldung.com/tag/spring-data-jpa)
Get started with Spring Data JPA through the reference Learn Spring Data JPA
course:
>> CHECK OUT THE COURSE (/learn-spring-data-jpa-course)
htipsifwnww.baeldung,comMhe-persistonce-layer-with-spring-dala-pa veoat2023 19:33, Introduction to Spring Data JPA| Baeldung
“
svitation 3 Energy Class Factory Gagner du temps et de Fagent
Hv:
rm Contact
1, Overview
‘This tutorial wll focus on introducing Spring Data JPA into a Spring project, and fully configuring the persistence
layer, For a step-by-step introduction to setting up the Spring context using Java-based configuration and the
basic Maven pom for the project, see this article (/bootstraping-a-web-application-with-spring-andl-java-based-
configuration)
Further reading:
Guide to JPA with Spring (/the-persistence-layer-with-spring-and-jpa)
Setup JPA with Spring -howto set up the EnttyManager factory and use the raw JPA APIS.
Road more (/the-
sistonce-layor-with-spring-and-jpa) ->
CrudRepository, JpaRepository, and PagingAndSortingRepository in Spring Data
(/spring-data-repositories)
Leatn about the diferent Navours of repositories offered by Spring Data,
Read more (/spring-data-repositories) >
Simplify the DAO with Spring and Java Generics (/simplifying-the-data-access-
layer-with-spring-and-java-generics)
Simplily the Data Access Layer by using a single, generifled DAO. which wil resultin strongpelegant data
access, no unnecessary elute
Read more (/simplifying-the-d
.ecess-layer-with-spring-and-java-generics) >
2. The Spring Data Generated DAO - No More DAO Implementations
‘As we discussed in an earlier article, the DAO layer V/simplifying-the-data-access-layer-with-spring-and-java-
generics) usually consists of alot of boilerplate code that can and should be simplified. The advantages of such a
simplification are many: a decrease in the number of artifacts that we need to define and maintain, consistency of
data access patterns, and consistency of configuration,
Spring Data lakes this simplification one step further and makes It possible to remove the DAO implementations
entirely. The interface of the DAO is now the only arlifact that we need lo explicitly define.
In order to start leveraging the Spring Data programming model with JPA. a DAO interface needs to extend the JPA
specific Repository interface, JpaRepository. This will enable Spring Data to find this interface and automatically
create an implementation fort.
By extending the interface. we get the most relevant CRUD methods for standard data access available in a
standard DAO.
hitpsslvww-baeldung.comithe-persistance-layer-wih-spring-datajpa 28oat2023 19:33, Introduction to Spring Data JPA| Baeldung
“
Des fonctionnalités qui stimulent
innovation et la valeur ajoutée pour
les clients
1M IBM
3. Custom Access Method and Queries
‘As discussed. by implementing one of the Repository interfaces, the DAO will already have some basic CRUD
methods (and queries) defined and implemented.
‘To define more specific access methods, Spring JPA supports quite a few options
«+ simply define a new method in the interface
++ provide the actual JPQL query by using the @Query annotation
‘+ use the more advanced Specification and Querydsl support in Spring Data
‘+ define custom queries via JPA Named Queries
‘The third option (http/springio/blog/2011/04/26 /adivanced-spring-data-jpa-specifications-and-querydsU/
Specifications and Queryds| support. is similar to JPA Criteria, but uses a more flexible and convenient APL This
‘makes the whole operation much more readable and reusable, The advantages of this API will become more
pronounced when dealing with a large number of fixed queries, as we could potentially express these more
concisely through a smaller number of reusable blocks.
‘The last option has the disadvantage that i either involves XML or burdening the domain class with the queries.
3.1. Automatic Custom Queries
‘When Spring Data creates a new Repository implementation, it analyses all the methods defined by the interfaces
and tres to automatically generate queries from the method names, While ths has some limitations, its avery
powerful and elegant way of defining new custom access methods with very title eft
Des fonctionnalités qui stimulent
innovation et la valeur ajoutée pour
les clients
1M IBM
Lets look at an example. Ifthe entity has a name field (and the Java Bean standard getName and setNane
methods), well define the findByName method in the DAO interface. This will automatically generate the correct
‘query:
public interface TFodD#0 extends JpeReposttory 0
Teactcsoing sce peverttntae
Serstopac tors
creep
htipsifwnww.baeldung,comMhe-persistonce-layer-with-spring-data-jpa secart2008 18.38 Inveduetonto Spring Data JPA| Badung
8. Using Spring Root,
We can also use the Spring Boot Starter Data JPA.
{https://munrepository.com/artifact /org.springframework boot /spring-boot-starter-data-jpa) dependency
that will automatically configure the DataSource for us.
We need to make sure that the database we want to use is present in the classpath, In our example, we've added.
the H2 in-memory database:
0
“groupla>org. spr ingfranework. boots groupe
spring-coot-starter-data~jpac/ertifectlo>
«
‘grovaTo>con.hadatabese
cartifectlobhac/artifactle>
everstor>1,4.209¢/verston>
«reependency>
‘Asa result just by doing these dependencies, our application is up and running and we can use it for other
database operations,
‘The explicit configuration for a standard Spring application is now included as part of Spring Boot auto
configuration.
We can, of course, modify the auto-configuration by adding our customized explicit configuration,
Spring Boot provides an easy way to do this using properties in the application properties fle. Let's see an example
of changing the connection URL and credentials
Bingdnteroreciuserto-t i
9. Useful Tools for Spring Data JPA
Spring Data JPA is supported in all major Java IDES. Let's see what useful tools are available for Eclipse and IntelliJ
IDEA,
Ifyou use Eclipse as your IDE, you can install the Dali Java Persistence Tools
{https:// wwweclipse. org/ webtools /dali downloads php) plugin. This provides ER diagrams for JPA entities,
DDL generation to initialize schema, and basic reverse engineering capabilities Also, you can use Eclipse Spring
te (STS). It will help to validate query method names in Spring Data JPA rapositories
Incase you use IntelliJ IDEA, there are two options,
Intel) IDEA Ultimate enables ER diagrams, a JPA console for testing JPOL statements, and valuable inspections.
However, these features are not available in the Community Edition
‘To boost productivity in Intell, you can install the JPA Buddy (https://plugins jetbrains.com/plugin/15075-
Jpa-buddy) plugin, which provides many foaturos, including goneration of JPA entitos, Spring Data JPA
repositories, DTOs, niaization DDL scripts Flyway versioned migrations, Liqubase changelogs. etc. Also, JPA
Buddy provides an advanced tool for reverse engineering.
v
hitpsslvww-baeldung.comithe-persistance-layer-wih-spring-datajpa oeoxt2023 19.98 lnvoducion a Spng Data JPA| Banting
Finally the JPA Sud aluain wey with both Community and Ultimate edtions,
10. Conclusion
Inthis article, we covered the configuration and implementation of the persistence layer with Spring 5, JPA 2, and
‘Spring Data JPA (part of the Spring Data umbrella project) using both XML and Java-based configuration.
We discussed ways to define more advanced custom queries, as well as transactional semantics, and 2
configuration with the new jpa namespace. The final result is a new and elegant take on data access with Spring,
‘with almost no actual implementation work
‘The implementation of this Spring Data JPA tutorial can be found in the GitHub project
{https://github.com/eugenp /tutorials/1ree/master/persistence-modules/spring-data-jpa-repo-2
Get started with Spring Data JPA through the reference Learn Spring Data
JPA course:
>> CHECK OUT THE COURSE (/learn-spring-data-jpa-course#table)
‘An intro to Spring Data, JPA
and Transaction Semantics Details with JPA
Get Persistence Right with Spring
Download the E-book (/persistence-with-spring)
‘Comments are closed on this article!
v
htipsifinww.bacidung.comMhe-persistonce-layer-with-spring-data-pa 718oat2023 19:33, Introduction to Spring Data JPA| Baeldung
“
‘COURSES
SERIES
‘ABOUT
htipsifwnww.baeldung,comMhe-persistonce-layer-with-spring-data-jpa