The document discusses the Repository interface and the use of Derived Queries in JPA, which automatically generate queries based on method naming conventions for GET/REMOVE operations. It also addresses pagination and sorting, JPQL syntax, and strategies to optimize queries involving multiple parent-child relationships, such as using JOIN FETCH and @BatchSize. Additionally, it covers the usage of @Modifying and @Transactional annotations, as well as the importance of Flush and Clear in managing the persistence context.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0 ratings0% found this document useful (0 votes)
6 views33 pages
SpringBoot JPA-Part 8
The document discusses the Repository interface and the use of Derived Queries in JPA, which automatically generate queries based on method naming conventions for GET/REMOVE operations. It also addresses pagination and sorting, JPQL syntax, and strategies to optimize queries involving multiple parent-child relationships, such as using JOIN FETCH and @BatchSize. Additionally, it covers the usage of @Modifying and @Transactional annotations, as well as the importance of Flush and Clear in managing the persistence context.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 33
29 PA pons
Till now, our Repository interface looks like this
‘And in service class, we used to invoke methods which are available in JPA frameworkThen, we have something called: Derived Query
‘+ Automatically generates queries from the methods.
‘+ Need to follow a specific naming convention,
«Derived query used for GET/REMOVE operations but not for INSERT/UPDATE
+ Insert and Update operations is supported though "savel)"
PartTree.java
(ind read |get|query| search| stream | count exists |delete| remove) (\\p(Lu.*2))??8y/ |
Method name should start with either One of CorMore
these values :find or read or get et characters
Uppercase Letter (ex: A8,C etc.) By’ atthe end of
the String
Query in which it get translates too:APorter
Pree
‘Comparison:
Part javaoo)Delete:
+ Need to add @Transactional annotation.JPA provides 2 interfaces to support Pagination and Sorting i.e.
Pageable Sort
pageNumber
ageSize ino recrasIf we need more info about Pages, then we can use "Page" as return typePaginations with Sorting:Only Sorting:* Sort-by accepts multiple fields.
* When multiple fields provided, sorting applied in order.* first it sort by first field and if there are duplicates then second field is used and so on.
* If we need different sorting order for different fieldsJava Persistence Query Language.
Similar to SQL but works on Entity Object instead of direct database.
Its database independent
- Works with Entity name and fields and not with table column names.
syntax:
Ent alia, returns the fess
*This san entity Meld name,
nota column name
This is anentity, nota table name
| binds, method
parameter
‘with named
parameter in
the query
JPQL query with JOIN
+ OneToOnewe don’t, want Object[] to be used, we can also return direct custom DTO
* OneToManyN+ Problem and its Solution:ablem
¥, 1 User can have Many Addresses.
dour Query is such that, it can fetch more than 1 Users. Then this problem can occurs.
, say we have 'N’ Users. Then below queries will be hit by JPA:
query to fetch all the USERS.
For each User it wll fetch ADDRESSES, so for N users, it will fetch N times.
total number of query hit : N+.
‘we need to find the way, so that only 1 QUERY it hit instead of N+1,
:0ing for the solution for this problem, One question might be coming to our mind:
|, we use EAGER initialization, then can we avoid this issue?
ause EAGER initialization do not work, when our query tries to fetch multiple PARENT
1d that also have multiple CHILD,
ous video, we tested EAGER with “findByID(id)" method, in which it make sure that, our
5 fetching only 1 PARENT and that can have many CHILD, that's fine. In that JPA internally
JOIN query.en Multiple parent with Multiple child get involved, EAGER do not work in just 1 query, it
cches all the parent and then for each parent, it fetch all its child
“> A query to fetch all userswith Name "AA
So it will return 2 users
For each user
Its Fetching alts addresses.
So for 2 users,
2 select query on child table
So, how to solve this, N#1 problem?
Solution’: using JOIN FETCH (JPQL)Solution2: using @BatchSize(size=10)
* Itwont make only 1 query, but it will reduce it, as it will divide it into batches
on3: using @EntityGraph(attributePaths="userAddressList")
2d over method (helpful in derived methods)LIPA to fetch all the entries of UserAddress along with user details
How to join Many tables?
Its almost same as SQL only
Say, we have
Table A has one to many relationship with Table 8
Table B has one to many relationship with Table C
“y{'SELECT a FROM Aa JOIN a.blist b JOIN b.clist c WHERE c.someProperty = :someValue")
findaWithBAndC{ @Param("someValue") String someValue};@Modifying Annotation
‘+ when @Query annotation used, by-default JPA expects SELECT query.
* If we try to use "DELETE" or "INSERT" or "UPDATE" query with @Query,
JPA will throw ercor, that
difying annotation, is to tell JPA that, expect either "DELETE" or "INSERT" or
ATE" query with @Query
‘we are trying to update the DB, we also need to use @Transactional
tation
derstanding Usage of Flush and Clear:
\s we know, Flush just pushed the persistence context changes to DB but
told the value in persistence context.clear, purge the persistence context, and required fresh DB callNow using, Flush and ClearPagination and Sorting in JPQL
Same like discussed in derived query method@NamedQuery Annotation
‘= We can name our Query, so that we can reuse it.