Spring Data JPA And Or Example Using Spring Boot

In this tutorial, we will see Spring Data JPA And Or 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 And Or 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

 

Query method using And.

public List<Student> findByNameAndRollNumber(String name, String rollNumber);

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_.name=?
and student0_.roll_number=?

Query method using Or.

public List<Student> findByNameOrRollNumber(String name, String rollNumber);

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_.name=?
or student0_.roll_number=?

 

Query method using And and Or together.

public List<Student> findByNameAndRollNumberOrUniversity(String name, String rollNumber, String university);

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_.name=?
and student0_.roll_number=?
or student0_.university=?

 

Repository Example.

 

Spring Data JPA And Or 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 – springdataandor , ArtifactId – springdataandor and name – springdataandor) 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 And Or Example

 

 

Student.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

 

Response Data.

 

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

 

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

http://localhost:9091/student/nameoandrollNumberoruniversity/john/121/rgtu

 

 

That’s all about Spring Data JPA And Or Example Using Spring Boot and oracle.

 

You may like.

 

Other Spring Data JPA and Hibernate tutorials.

 

Spring Data JPA Docs.

 

 

 

 

 

Top