🔍
Spring Data JPA: What is it? And Why Should You Use It? - YouTube
Channel: unknown
[1]
Hi, I’m Thorben Janssen from thoughts-on-java.org.
[4]
Today, I will talk about
[5]
Spring Data JPA and its relationship with Hibernate.
[8]
I will also show you why and how you can use it.
[11]
But before we proceed,
[13]
please subscribe and hit the bell icon
[14]
so that you don’t miss any future videos
[17]
on this channel which will help you
[18]
to improve your professional skills.
[21]
When you implement a new application,
[23]
you should focus on the business logic
[25]
instead of technical complexity and boilerplate code.
[28]
That’s why the JPA specification
[31]
and Spring Data JPA are extremely popular.
[34]
JPA handles most of the complexity of
[37]
JDBC-based database access
[39]
and object-relational mappings.
[41]
On top of that,
[42]
Spring Data JPA reduces the amount of
[44]
boilerplate code required by JPA.
[47]
That makes the implementation of your
[49]
persistence layer easier and faster.
[52]
Sounds great, doesn’t it?
[53]
It’s no surprise that a lot of development teams
[56]
use that stack to implement their persistence layer.
[59]
If you’re new to it, I’m happy to introduce you to
[62]
Spring Data JPA in this video.
[64]
I will: explain the relationship between Spring Data
[68]
JPA, JPA and Hibernate/EclipseLink
[70]
show you 3 reasons to use Spring Data JPA
[73]
and help you set up your first project
[76]
using Spring Data JPA
[78]
I will also give you a quick introduction to
[80]
Spring Data’s repositories.
[83]
And I already posted a
[84]
Getting started guide to JPA and Hibernate.
[87]
If you’re not familiar with the JPA specification,
[90]
please take a look at that other guide first.
[92]
I will add the link to it to the description.
[96]
Before we start talking about Spring Data JPA,
[99]
we should first discuss its relationship
[101]
to JPA, Hibernate, and EclipseLink.
[104]
JPA is a specification that defines an API
[107]
for object-relational mappings
[109]
and for managing persistent objects.
[112]
Hibernate and EclipseLink are
[114]
2 popular implementations of this specification.
[117]
You can learn more about the difference in my
[120]
What’s the difference between
[121]
JPA, Hibernate and EclipseLink Video.
[124]
I have added a link to it to the video description.
[128]
Spring Data JPA adds a layer on top of JPA.
[131]
That means it uses all features
[133]
defined by the JPA specification,
[135]
especially the entity and association mappings,
[138]
the entity lifecycle management,
[140]
and JPA’s query capabilities.
[142]
On top of that, Spring Data JPA adds its own features
[146]
like a no-code implementation of the repository pattern
[149]
and the creation of database queries
[151]
from method names
[153]
OK, so if the JPA specification
[155]
and its implementations provide most of the features
[158]
that you use with Spring Data JPA,
[161]
do you really need the additional layer?
[163]
Can’t you just use the Hibernate or EclipseLink directly?
[167]
You can, of course, do that.
[168]
That’s what a lot of Java SE applications do.
[171]
And Jakarta EE provides a good integration
[174]
for JPA without adding an extra layer.
[177]
But the Spring Data team took the extra step
[180]
to make your job a little bit easier.
[182]
The additional layer on top of JPA enables them to
[185]
integrate JPA into the Spring stack seamlessly.
[189]
They also provide a lot of functionality that
[191]
you otherwise would need to implement yourself.
[193]
Here are my 3 favorite features
[195]
that Spring Data adds on top of JPA.
[200]
The repository pattern is one of the
[202]
most popular persistence-related patterns.
[205]
It hides the data store specific implementation details
[208]
and enables you to implement your business code
[210]
on a higher abstraction level.
[213]
Implementing that pattern isn’t too complicated
[215]
but writing the standard CRUD operations
[217]
for each entity
[218]
creates a lot of repetitive code.
[221]
Spring Data JPA
[222]
provides you a set of repository interfaces
[224]
which you only need to extend to define a
[227]
specific repository for one of your entities.
[232]
I will show you Spring Data repositories in
[234]
more details at the end of this video.
[237]
Here is a quick example of a repository
[239]
that provides the required methods to
[242]
persist, update and remove one
[244]
or multiple Author entities,
[246]
to find one or more Authors by their primary keys,
[248]
to count, get and remove all Authors
[251]
and to check if an
[252]
Author with a given primary key exists.
[254]
And before you ask: Yes,
[256]
that code sample is correct and complete.
[259]
The CrudRepository interface
[261]
defines all methods I mentioned before.
[263]
So, you just need to extend it.
[268]
To make it even easier,
[269]
Spring Data JPA provides a default implementation
[272]
for each method defined by
[274]
one of its repository interfaces.
[276]
That means that you no longer need to
[278]
implement basic read or write operations.
[280]
And even so all of these operations
[283]
don’t require a lot of code,
[284]
not having to implement them
[286]
makes life a little bit easier
[287]
and it reduces the risk of stupid bugs.
[290]
Another comfortable feature of Spring Data JPA
[293]
is the generation of database queries
[295]
based on method names.
[297]
As long as your query isn’t too complex,
[299]
you just need to define a
[300]
method on your repository interface
[303]
with a name that starts with find…By.
[305]
Spring then
[307]
parses the method name and creates a query for it.
[313]
Here is a simple example of a query
[315]
that loads a Book entity with a given title.
[318]
Internally, Spring generates a
[319]
JPQL query based on the method name,
[322]
sets the provided method parameters
[324]
as bind parameter values,
[325]
executes the query and returns the result.
[331]
As you have seen, Spring Data JPA
[333]
can make the implementation of your persistence layer
[335]
much easier.
[337]
So, what do you have to do to use it in your application?
[340]
Not much, if you’re using Spring Boot
[342]
and structure your application in the right way.
[348]
You only need to add the
[349]
spring-boot-starter-data-jpa artifact,
[352]
and your JDBC driver to your maven build.
[355]
The Spring Boot Starter includes
[356]
all required dependencies
[358]
and activates the default configuration.
[361]
In the next step, you can configure your
[363]
database connection in the application.properties
[366]
or application.yml file.
[368]
If you use JPA outside of Spring,
[370]
you need to configure this
[372]
and a few other things in the persistence.xml.
[375]
Spring Boot and Spring Data JPA
[377]
handle the default configuration for you,
[380]
so that you only need to override the
[381]
parameters you want to change.
[384]
If you structure your project in the right way,
[386]
that’s all you need to do to be able
[388]
to use Spring Data JPA
[390]
and its repositories in your project.
[392]
By default, Spring Boot expects that all repositories
[396]
are located in sub-packages of the class annotated with
[399]
SpringBootApplication.
[401]
If your application doesn’t follow this default,
[403]
you need to configure the packages of your repositories
[406]
using an @EnableJpaRepositories annotation.
[412]
After setting everything up,
[414]
it’s time to take a closer look at repositories.
[416]
There are 3 repository interfaces
[418]
that you should know when you use Spring Data JPA:
[423]
As you might guess from its name,
[424]
the CrudRepository interface
[426]
defines a repository that offers standard
[428]
create, read, update and delete operations.
[431]
The PagingAndSortingRepository
[434]
extends the CrudRepository
[436]
and adds find All methods
[437]
that enable you to sort the result
[439]
and to retrieve it in a paginated way.
[442]
Both interface are also supported by
[444]
other Spring Data projects,
[446]
so that you can apply the same concepts
[448]
to different datastores.
[450]
The JpaRepository adds JPA-specific methods,
[453]
like flush() to trigger a flush on the persistence context
[456]
or findAll to find entities by example,
[459]
to the PagingAndSortingRepository.
[462]
You can use any of the standard interfaces
[465]
to define your own repository definition.
[467]
You, therefore, need to extend one of
[469]
Spring Data JPA’s interface,
[471]
for example the CrudRepository interfaces
[474]
and type it to the entity class and its primary key class.
[480]
Here you can see a simple example.
[482]
The Book entity is a normal JPA entity with a
[485]
generated primary key of type Long,
[487]
a title
[488]
and a many-to-many association to the Author entity.
[491]
If you want to define a CRUD repository for this entity,
[495]
you need to extend Spring Data JPA’s
[497]
CrudRepository interface
[498]
and type it to Book and Long.
[501]
And I also added the
[502]
findByTitle method to the repository
[504]
to find a Book entity by a given title.
[509]
After you defined your repository interface,
[511]
you can use the @Autowired annotation
[514]
to inject it into your service implementation
[517]
Spring Data will then provide you with a
[519]
proxy implementation of your repository interface.
[522]
This proxy provides default implementations
[525]
for all methods defined in the interface.
[527]
If you need to adapt the default functionality,
[530]
you can provide your own repository implementations.
[533]
But that is a topic for another video.
[535]
Let’s focus on Spring Data JPA’s standard functionality for now.
[542]
In your business code, you can then use the
[544]
injected repository to read entities from the database
[548]
and to persist new or changed entities.
[550]
The test class in the following code snippet
[553]
uses the BookRepository
[554]
to find a Book entity with the title ‘Hibernate Tips’
[557]
and to persist a new Book entity.
[562]
Spring Data JPA seamlessly integrates
[565]
JPA into the Spring stack,
[566]
and its repositories reduce the boilerplate code
[569]
required by the JPA specification.
[572]
It’s important to know that most features,
[574]
like the object-relational mapping
[576]
and query capabilities,
[578]
are defined and provided by the
[580]
JPA specification and its implementations.
[583]
That means that you can use all the features
[585]
of your favorite JPA implementation.
[587]
Spring Data JPA just makes using them easier.
[591]
OK, that’s it for today.
[593]
If you want to learn more about Hibernate,
[595]
you should join the free Thoughts on Java Library.
[598]
It gives you free access to a lot of member-only content
[600]
like a cheat for this video
[602]
and an ebook about using
[604]
native queries with JPA and Hibernate.
[606]
I’ll add the link to it to the video description below.
[609]
And if you like today’s video,
[611]
please give it a thumbs up and subscribe below.
[614]
Bye
Most Recent Videos:
You can go back to the homepage right here: Homepage





