Spring Data JPA: Ultimate Guide to Custom Queries with @Query Annotation

  Рет қаралды 86,812

Thorben Janssen

Thorben Janssen

4 жыл бұрын

In this video, I will show you how to define and use custom queries with Spring Data JPA. You will learn how to execute JPQL and native queries, use an expression language in your queries and execute queries that INSERT, UPDATE, or REMOVE records.
But before we start, if this is your first time here, and you want to learn how to create your entity mappings with ease, build incredible efficient persistence layers with Hibernate and all types of other Java persistence related stuff, start now by subscribing and clicking the bell, so you don't miss anything.
Derived queries are very comfortable to use as long as the queries are not too complicated. But as soon as you use more than 2-3 query parameters or need to define multiple joins to other entities, you need a more flexible approach. In these situations, you better use Spring Data JPA’s @Query annotation to specify a custom JPQL or native SQL query.
If you like this video, please give me your thumbs up and share it with your friends and co-workers.
Like my channel? Subscribe!
➜ bit.ly/2cUsid8
Join the free Member Library:
goo.gl/dtyIIC
Read the accompanying post: thoughts-on-java.org/spring-d...
Want to connect with me?
Blog: www.thoughts-on-java.org/
Twitter: / thjanssen123
Facebook: / thoughtsonjava
#JPA #Spring

Пікірлер: 48
@MsPrettyg00d
@MsPrettyg00d 3 жыл бұрын
Your instructions are clear and short👍. Thank you!
@DreamingWithEyesWide
@DreamingWithEyesWide Жыл бұрын
Best videos about JPA- thanks for making them!!
@4jilson
@4jilson 4 жыл бұрын
Your videos are good, this deserves more likes and views.
@isurangaediriweera7073
@isurangaediriweera7073 4 жыл бұрын
Your videos are really helpful. Thank you very much.
@luisdavidherreragarcia281
@luisdavidherreragarcia281 3 жыл бұрын
thank you for sharing your knowledge !
@sunilcd2008
@sunilcd2008 2 жыл бұрын
Thank you Thorben! Very well explained and pretty clear about each concept!
@ThoughtsOnJava
@ThoughtsOnJava 2 жыл бұрын
Thanks
@pisteuo_creciendo_juntos
@pisteuo_creciendo_juntos 3 жыл бұрын
thank you, grettings from chile
@tiituma
@tiituma 3 жыл бұрын
Thank you so much, this video helped a lot
@nishantbahri
@nishantbahri 4 жыл бұрын
That was precise.
@paolofrancescosciammarella1420
@paolofrancescosciammarella1420 2 жыл бұрын
thank u so much!
@ricardo.fontanelli
@ricardo.fontanelli 3 жыл бұрын
The best videos about spring and jpa, really well organized and explained, pure gold thank you
@manuelkyle6591
@manuelkyle6591 2 жыл бұрын
You prolly dont care but does anyone know of a method to get back into an Instagram account..? I was stupid lost the password. I would appreciate any assistance you can give me!
@jaggerowen4148
@jaggerowen4148 2 жыл бұрын
@Manuel Kyle instablaster :)
@manuelkyle6591
@manuelkyle6591 2 жыл бұрын
@Jagger Owen I really appreciate your reply. I found the site through google and I'm in the hacking process now. Takes a while so I will get back to you later with my results.
@manuelkyle6591
@manuelkyle6591 2 жыл бұрын
@Jagger Owen it did the trick and I actually got access to my account again. Im so happy! Thank you so much you saved my account!
@jaggerowen4148
@jaggerowen4148 2 жыл бұрын
@Manuel Kyle You are welcome :)
@kthun-v7c
@kthun-v7c 7 күн бұрын
0:00 Intro 1:42 JPQL Queries 4:18 Sorting 5:52 Paginating Query Results 6:49 SpEL Expressions for Entity Names & Advabced Like Expressions 8:24 Native Queries 9:53 Parameter Handling 12:13 Modifying Queries 13:00 Conclusion 14:25 Thoughts on Java Library
@sonofgod00
@sonofgod00 Жыл бұрын
Thanks
@varunraj1862
@varunraj1862 8 ай бұрын
Great video. May i know how to reference the fields declared indide the entity using SPeL. It will be really helpful.
@MsPaulisa
@MsPaulisa 4 жыл бұрын
How do you use a list parameter with columns that you want to select? I tried something like "select ?3 from products where category = ?1 and status = ?2" . And ?3 is a List with the columns that I want to select... but it does not work...
@abhinavrayakwar6676
@abhinavrayakwar6676 4 жыл бұрын
While updating or altering the table with paramerter , spring jpa add extra quotes in query " ' " this single quote help
@vtdx4
@vtdx4 3 жыл бұрын
when we bind parameters in case of my parameter is an object how can I bind one property of the object inside query. can I access it with dot notation
@NitrosS
@NitrosS 3 жыл бұрын
if you wanna insert use this: @Modifying @Query(value = "insert into author(first_name, last_name, version) values(?#{#author.firstName}, ?#{#author.lastName}, ?#{#author.version})",nativeQuery = true) void insert(@Param("author") Author author); //SpEL Expressions You have to use @Modifying and maybe @Transactional. There is another method to insert and it's the EntityManager.
@gamatossian
@gamatossian 3 жыл бұрын
I think there's a small error there @ 12:00? @Param("lastName") String firstName, @Param("firstName") String lastName Assuming it should be: @Param("firstName") String firstName, @Param("lastName") String lastName
@NitrosS
@NitrosS 3 жыл бұрын
insert is possible here? I'm looking for insertion
@esperanzacutillasrastoll6722
@esperanzacutillasrastoll6722 3 жыл бұрын
how do you do a left join with that? Thank you. With to related entities with a one to one relationship
@ThoughtsOnJava
@ThoughtsOnJava 3 жыл бұрын
You can use all features of JPQL in your custom query. LEFT JOIN is one of them. Find out more here: thorben-janssen.com/jpql/
@ianwanjala8621
@ianwanjala8621 7 ай бұрын
hey, this is very good content, but the link provided for accessing the free java thoughts materials is not working, could I get those materials? Thanks again!
@lokeshmurukesh3386
@lokeshmurukesh3386 3 жыл бұрын
can we do "select into" query in @Query ?
@chetanbhandari3033
@chetanbhandari3033 4 жыл бұрын
Hi Video is too good pls provide github link for source code
@UTUBDZ
@UTUBDZ 3 жыл бұрын
Guten Tag Janssen ! Can you give a hint on how to write a JPQL queries when the WHERE close contains objects (Ex: SELECT * FROM USERS WHERE userBooks.count(*) > 10)
@ThoughtsOnJava
@ThoughtsOnJava 3 жыл бұрын
Is userBooks a managed association? If it is, you could use the size function to count the elements in it, e.g.: SELECT u FROM Users WHERE SIZE(u.userBooks) > 10
@UTUBDZ
@UTUBDZ 3 жыл бұрын
​@@ThoughtsOnJava Thank you very much for your reply.
@maheshlokavarapu9696
@maheshlokavarapu9696 3 жыл бұрын
Could you please provide oracle r2dbc video series
@memillin
@memillin Жыл бұрын
12:06 It seems that named bind parameters are flipped.
@senthilmurugangsm
@senthilmurugangsm 4 жыл бұрын
How can we select few columns in @Query annotations and return the list of entities with only those specific columns as JSON. (Not all values of entity)
@ThoughtsOnJava
@ThoughtsOnJava 4 жыл бұрын
Hi Senthil, you can only select an entity with all its attributes. If you want to select a subset, you need to use a DTO projection. I explain that in this video: kzbin.info/www/bejne/f4G1l61vbJKBp6M In the video, I only use Hibernate without Spring Data JPA. But you can apply the same approach using Spring. Regards, Thorben
@senthilmurugangsm
@senthilmurugangsm 4 жыл бұрын
@@ThoughtsOnJava thanks for the response. Is any other way available by without creating another pojo class ?
@rajuTravelMemories
@rajuTravelMemories 10 ай бұрын
Could u please help me on thi Caused by: java.lang.IllegalArgumentException: Can't compare test expression of type [CommodityEntity] with element of type [BasicSqmPathSource(materialId : Long)]
@ursfren
@ursfren 3 жыл бұрын
How to index pdf content by using spring-data? We will really appreciate if some one can answer this question.
@MrProton2
@MrProton2 4 жыл бұрын
How Spring Data JPA Native Querry worked with SELECT FOR UPDATE SKIP LOCKED ?
@amitdhakal1974
@amitdhakal1974 3 жыл бұрын
But no customized delete option in the query
@davincibaker
@davincibaker 20 күн бұрын
WHAT ABOUT INSERTS ?
@treeteqs
@treeteqs 4 жыл бұрын
what about @restresource
@yadigarcaliskan6453
@yadigarcaliskan6453 7 ай бұрын
Why does spring try to overtake the industry? I don’t like
@chessmaster856
@chessmaster856 6 ай бұрын
There is no complex query
@sidof8065
@sidof8065 Ай бұрын
Thanks for the content ! how write jUnit test of class with enum properties. Like public interface HouseRepository extends PagingAndSortingRepository { Page findByHouseTypeContaining(HouseType houseType, Pageable pageable); } @Entity(name = "house") public class House { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "house_sequence") @SequenceGenerator(name = "house_sequence",sequenceName = "house_sequence",allocationSize = 1) private Long id; @Enumerated(STRING) private HouseType houseType;
Ultimate Guide: Derived Queries with Spring Data JPA
10:14
Thorben Janssen
Рет қаралды 14 М.
Spring Data JPA from 0-100 in 60 minutes
1:02:31
VMware Tanzu
Рет қаралды 81 М.
How Many Balloons Does It Take To Fly?
00:18
MrBeast
Рет қаралды 37 МЛН
THEY WANTED TO TAKE ALL HIS GOODIES 🍫🥤🍟😂
00:17
OKUNJATA
Рет қаралды 21 МЛН
Spring Data JPA -Criteria Queries - The Full Guide
30:07
Bouali Ali
Рет қаралды 36 М.
How to call native SQL queries with JPA and Hibernate
13:46
Thorben Janssen
Рет қаралды 4,2 М.
Пример реактивного приложения на Spring WebFlux
1:52:10
Сергей Петрелевич
Рет қаралды 8 М.
Spring Data JPA: Query Projections
10:10
Thorben Janssen
Рет қаралды 18 М.
Spring Boot Tutorial | Full In-depth Course
3:11:18
Daily Code Buffer
Рет қаралды 521 М.
Learn Dependency Injection and Write Better Code
21:52
Amigoscode
Рет қаралды 168 М.
5 JPA Features That Are Easier To Use with Spring Data JPA
8:04
Thorben Janssen
Рет қаралды 8 М.
Мой инст: denkiselef. Как забрать телефон через экран.
0:54
Опять съемные крышки в смартфонах? #cmf
0:50
Samsung Galaxy 🔥 #shorts  #trending #youtubeshorts  #shortvideo ujjawal4u
0:10
Ujjawal4u. 120k Views . 4 hours ago
Рет қаралды 3,8 МЛН
Собери ПК и Получи 10,000₽
1:00
build monsters
Рет қаралды 2,4 МЛН
Первый обзор Galaxy Z Fold 6
12:23
Rozetked
Рет қаралды 401 М.