Spring Data JPA Nested Property Query Method

In this tutorial, we will see Spring Data JPA Nested Property Query Method Example using Spring Boot and oracle.

Let’s see how to define the Query method(Query creation from method names) for Nested Property in Spring Data JPA.

Consider we have two entities Student.java and Address.java. Student and Address entities are in one to one relationship and we want to fetch all students from the database who belongs to city pune.

Student.java

 

Address.java

 

And we have some record in student and address table as below.

Spring Data JPA Nested Property Query Method Example

 

 

 

Query method to fetch all students who belong to city pune using Spring Data JPA.

List<Student> findByAddressCity(String city);

Generated Query –

Hibernate:
select
address0_.id as id1_0_0_,
address0_.city as city2_0_0_,
address0_.house_number as house_number3_0_0_
from
address address0_
where
address0_.id=?

 

 

Spring Data JPA Nested Property Query Method 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 – springdatanestedproperty , ArtifactId – springdatanestedproperty and name – springdatanestedproperty) 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 Nested Property Query Method Example

 

Let’s Define entities for Spring Data JPA Nested Property Query Method Example.

Student.java

Address.java

Let’s Define controller for Spring Data JPA Nested Property Query Method Example.

 

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/address/pune

Spring Data JPA Nested Property Query Method Example

That’s all about Spring Data JPA Nested Property Query Method Using Spring Boot and oracle.

 

You may like.

 

Other Spring Data JPA and Hibernate tutorials.

 

Spring Data JPA Docs.

 

 

Top