Spring Data JPA @NamedNativeQuery

In this tutorial, we will see Spring Data JPA @NamedNativeQuery annotation Example using Spring Boot and oracle. Also, we will see how to write multiple @NamedNativeQuery using @NamedNativeQueries annotation.

@NamedNativeQuery and @NamedNativeQueries annotations used with entity class. Using @NamedNativeQuery annotation we define a query. Using @NamedNativeQueries we can define multiple @NamedNativeQuery.

 

Consider we have an entity called Student.java as below.

 

and we have some record in the database as below.

Spring Data JPA @NamedNativeQuery

 

Using @NamedNativeQuery.

Let’s define Spring Data JPA @NamedNativeQuery and Spring Data JPA @NamedNativeQueries.
Using @NamedNativeQueries.

Note – If we don’t define resultClass = Student.class then we may not get the proper response.

For example – You may get response data in below format.

http://localhost:9091/student/findByName/john

 

Sample Repository Example.

Till now We have seen Spring Data JPA @NamedNativeQuery and @NamedNativeQueries annotations. Let’s see a complete example from scratch.

Spring Data JPA @NamedNativeQuery and  @NamedNativeQueries Example using  Spring Boot and Oracle.

Open eclipse and create maven project, Don’t forget to check ‘Create a simple project (skip)’ click on next.  Fill all details(GroupId – springdatanamednativequery, ArtifactId – springdatanamednativequery and name – springdatanamednativequery) and click on finish. Keep packaging as the jar.

Modify pom.xml

Note – In pom.xml we have defined javac.exe path in configuration tag. You need to change accordingly i.e where you have installed JDK.

If you see any error for oracle dependency then follow these steps.

Directory structure –

Spring Data JPA @NamedNativeQuery

 

Let’s seWe will see about Spring Data JPA @NamedNativeQuery and Spring Data JPA @NamedNativeQueriese 

Student.java

StudentController.java

Repository for Spring Data JPA @NamedNativeQuery and Spring Data JPA @NamedNativeQueries.

Note – See more details about @Controller and RestController here.

StudentRepository.java – interface

StudentService.java – interface

Note – See here more about @Component, @Controller, @Service and @Repository annotations here.

StudentServiceImpl.java

SpringMain.java

Note – See more details about @ComponentScan here.

JpaConfig.java

Note – See more details about @Configuration annotations here.

application.properties

Let’s run the SpringMain class(run as java application).

Perform saveall operation first using below REST API.

http://localhost:9091/student/saveall

Response Data.

 

http://localhost:9091/student/findByName/john

 

http://localhost:9091/student/findByNameAndRollNumber/john/120

Spring Data JPA @NamedNativeQuery

 

http://localhost:9091/student/findByNameOrRollNumber/john/121

 

 

That’s all about Spring Data JPA @NamedNativeQuery and @NamedNativeQueries Example Using Spring Boot and Oracle.

Let’s see what are the fields defined inside @NamedNativeQuery.

Fields defined inside @NamedNativeQueries.

 

You may like.

 

Other Spring Data JPA and Hibernate tutorials.

 

Spring Data JPA Docs.

 

Summary –  We have seen about Spring Data JPA @NamedNativeQuery and @NamedNativeQueries.

Top