Spring Data JPA In and NotIn Example

In this tutorial, we will see Spring Data JPA In and NotIn Example using Spring Boot and oracle. We will see what query prepared in both cases with example.

Let’s see how to define the Query method(Query creation from method names) using Spring Data JPA In and NotIn Keywords.

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

 

and we have some record in the database as below.

Spring Data JPA And Or Example

 

Spring Data JPA Query method using In Keyword.

public List<Student> findByRollNumberIn(List<String> rollNumbers);

Generated Query.

 

Hibernate:
select
student0_.id as id1_0_,
student0_.name as name2_0_,
student0_.roll_number as roll_number3_0_,
student0_.university as university4_0_
from
student student0_
where
student0_.roll_number in (
? , ?
)

Spring Data JPA Query method using NotIn Keyword.

public List<Student> findByRollNumberNotIn(List<String> rollNumbers);

Generated Query.

Hibernate:
select
student0_.id as id1_0_,
student0_.name as name2_0_,
student0_.roll_number as roll_number3_0_,
student0_.university as university4_0_
from
student student0_
where
student0_.roll_number not in (
? , ? , ?
)

 

 

Repository Example.

 

Spring Data JPA In and NotIn 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 – springdatainnotin, ArtifactId – springdatainnotin and name – springdatainnotin) 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 In and NotIn Example

 

 

Student.java

IdContainer.java

 

StudentController.java

 

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

Let’s see response data in Spring Data JPA In and NotIn Example using Spring Boot and Oracle.

Response Data.

 

http://localhost:9091/student/in/rollnumbers

request data

Response –

http://localhost:9091/student/notin/rollnumbers

Request Data –

Response –

 

That’s all about Spring Data JPA In and NotIn Example Using Spring Boot and oracle.

 

You may like.

 

Other Spring Data JPA and Hibernate tutorials.

 

Spring Data JPA Docs.

 

 

 

Top