filmov
tv
Spring Data JPA : How to write custom query by JpaRespository | Native Query
Показать описание
#JpaRespository #CustomQuery
► Java: Write custom query with @Query & @NamedQuery annotation by Spring Data JPA
► SUBSCRIBE & LIKE!!
► Download the sample java microservice application :
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
► Here is our amazing playlist for Core Java, Spring MVC/Boot, Git and Micro service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Watch my "Most Watched Videos"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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:
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 :
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(
nativeQuery = true)
Collection(UserDetails) findAllActiveUsersNative();
Regards,
Debu Paul
► Java: Write custom query with @Query & @NamedQuery annotation by Spring Data JPA
► SUBSCRIBE & LIKE!!
► Download the sample java microservice application :
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
► Here is our amazing playlist for Core Java, Spring MVC/Boot, Git and Micro service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Watch my "Most Watched Videos"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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:
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 :
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(
nativeQuery = true)
Collection(UserDetails) findAllActiveUsersNative();
Regards,
Debu Paul
Комментарии