How to query alter domain models by Spring JPA Projection – SpringBoot + MySQL database

When you query with Spring Data, normally the results are full domain models. But in various cases, we need to alter the returned data with another view by customized fields, how to do it?
-> In the tutorial, JavaSampleApproach will show a solution to modify returned domain model by Spring JPA Projection.

Related articles:
How to configure Spring JPA One to One Relationship – SpringBoot

I. Technologies

Java 1.8
Maven
Spring Tool Suite
Spring Boot
Spring JPA
MySQL database

II. Spring JPA Projection

Create 2 Entities: Wife and Husband that have One to One Relationship:

Wife model:

Husband model:

Then create a Jpa Wife repository as below:

When invoking the function: findWifeByFirstName(String firstName), we will get a returned model Wife with full 4 fields: {firstName, lastName, age, husband}.
But we need customize the returned model with just 3 fields: {firstName, lastName, age}.
What is the solution?
-> Spring JPA Projection will resolve the problem.

We create a Jpa Projection interface as below:

interface WifeInfo

Then modify the returned data-type of Jpa query:

Spring JPA Projection can help us to adjust the exposed data model? -> Yes, We can Re-Modelling data by adding virtual properties with the support of @Value. See the below Spring JPA Re-modelling Projection:

Then use RemodellingWifeInfo with below Jpa Query:

III. Practice

We create a Spring JPA project with 2 entities: Wife and Husband that have one-to-one relationship. Then using 2 Spring JPA Projection interfaces: WifeInfo and RemodellingWifeInfo to alter domain model for query-data.

Related articles: Spring JPA One to One Relationship – SpringBoot

spring jpa projection - project structure

Step to do:
– Create SpringBoot project
– Create Entity Models
– Create Spring JPA Projection
– Create Spring JPA repositories
– Export some RestAPIs
– Implement Clients
– Deployment

1. Create SpringBoot project

Using Spring Tool Suite to create a Spring Starter Project, then add dependencies {data-jpa, web, mysql-connector-java}:

2. Create Entity Models

Wife entity:

Husband entity:

3. Create Spring JPA Projection

WifeInfo projection:

RemodellingWifeInfo projection:

4. Create Spring JPA repositories

– Create WifeRepository with 2 projections {WifeInfo, RemodellingWifeInfo}:

– Create HusbandRepository:

– Open file application.properties, configure spring data-source and spring.jpa:

5. Export some RestAPIs

Export 2 RestAPIs {/findwife, /findremodelwife}:
/findwife will return WifeInfo.
/findremodelwife will return RemodellingWifeInfo.

Now, create WebController with above RestAPIs:

6. Implement Clients

Implement simple Clients to initialize data with 3 functions:

deleteData()
saveData()
showData()

7. Deployment

Build and Run the project by below commandlines: mvn clean install and mvn spring-boot:run.

-> First logs:

Make a request: http://localhost:8080/findwife?firstname=Mary

-> Result:

-> Logs:

Make a request: http://localhost:8080/findremodelwife?firstname=Mary

-> Result:

-> Logs:

IV. Sourcecode

SpringJPAProjection

By JavaSampleApproach | June 13, 2017.


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*