Duration 17:29

Spring Data JPA : How to write custom query by JpaRespository | Native Query

38 873 watched
0
453
Published 9 May 2020

#JpaRespository #CustomQuery ► Java: Write custom query with @Query & @NamedQuery annotation by Spring Data JPA ► SUBSCRIBE & LIKE!! ► Official Email Id: techtalk.debu@gmail.com ► Download the sample java microservice application : https://github.com/admindebu/In-Memory-DataBase-Rest-API ► Follow on Facebook: https://www.facebook.com/TechTalkDebu ► Follow on LinkedIn: https://www.linkedin.com/in/debu-paul ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ► Here is our amazing playlist for Core Java, Spring MVC/Boot, Git and Micro service ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1. Core Java :: /watch/lOGbK-VZ0IRpdS47rrLp5o6kodKy3TlRLP=tsil&Ue_Iko0h_RIhb 2. Spring MVC & Spring Boot :: /watch/8b_PxgFwNrIupl-6O_0dFr6kodKy3TlRLP=tsil&oifC8t1kW-fkP 3. Micro Service :: /watch/7K12TaiiW4PT57R0Y4j6_r6kodKy3TlRLP=tsil&IntGdLaycspy2 4. Git/GitHub :: /watch/7jnL6ySbJ6ZHPmI7rapVjo6kodKy3TlRLP=tsil&E4lnEyABxHXBL ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Watch my "Most Watched Videos" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ► HTTPS & HTTPS protocol :: /watch/zWrcOUNiwanae3MGGB3Zvo6kodKy3TlRLP=tsil&89wV0DSSnlBSc Playlist : /watch/b602K_lGqscBNwL85XhwLp6kodKy3TlRLP=tsil&w-odEsrxoFCx2 Tutorial for beginners with examples, Interview Questions and Concepts. In order to define SQL to execute for a Spring Data repository method, we can annotate the method with the @Query annotation — its value attribute contains the JPQL or SQL to execute. The @Query annotation takes precedence over named queries, which are annotated with @NamedQuery or defined in an orm.xml file. It's a good approach to place a query definition just above the method inside the repository rather than inside our domain model as named queries. The repository is responsible for persistence, so it's a better place to store these definitions. 2.1. JPQL By default the query definition uses JPQL. Let's look at a simple repository method that returns active User entities from the database: @Query("SELECT u FROM User u WHERE u.status = 1") Collection(User) findAllActiveUsers(); Please note: if you are using JPQL then you have to provide the exact Entity name in the entity class. else you will get below exception : org.hibernate.hql.internal.ast.QuerySyntaxException: table is not mapped Spring, Spring Data JPA: org.hibernate.hql.internal.ast.QuerySyntaxException: Test is not mapped Native We can use also native SQL to define our query. All we have to do is to set the value of the nativeQuery attribute to true and define the native SQL query in the value attribute of the annotation: @Query( value = "SELECT * FROM UserDetails u WHERE u.status =?1", nativeQuery = true) Collection(UserDetails) findAllActiveUsersNative(); Regards, Debu Paul

Category

Show more

Comments - 54