Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

JPA - Basic Projections

DZone's Guide to

JPA - Basic Projections

Projection is a subset of entities' properties, represented as dedicated class(es), and mapped either as the database view based entity, or using constructor expressions.

· Java Zone
Free Resource

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

In my last post: JPA - Should I become a laziness extremist? - I mentioned about the possibilities of improving JPA usage - one of them is using Projections.

Projection is a subset of entities' properties. It can be represented as dedicated class (or classes), and mapped either as the database view based entity, or using constructor expressions [1][2]. The clue of this solution is having very limited entities tree (or no tree at all, as in my example) comparing to original entity (Employee). We need to display employee name, thus we build the projection having employee name and ID only. As you will see below, using projections leads to single SQL query, instead of bunch of SQL queries (see example in JPA - Should I become a laziness extremist?)

Database view based entities

What we need in this case is simple JPA entity, mapped to any database view (or table if desired properties are in one table).

package com.blogspot.vardlokkur.domain;

import java.io.Serializable;

import javax.annotation.concurrent.Immutable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Immutable
@Table(name = "EMPLOYEE")
public class ViewBasedEmployeeNameProjection implements Serializable {

@Column(name = "EMPLOYEE_ID")
@Id
private Long employeeId;

@Column(name = "EMPLOYEE_NAME")
private String name;

ViewBasedEmployeeNameProjection() {
super();
}

public Long getEmployeeId() {
return employeeId;
}

public String getName() {
return name;
}

}

Now we can run JPQL query: select employee from ViewBasedEmployeeNameProjection employee order by employee.name

which in turn will execute one single SQL query:

select EMPLOYEE_ID, EMPLOYEE_NAME from EMPLOYEE order by EMPLOYEE_NAME
Using this method you have to remember that you can choose anything for the projection ID, as long as it uniquely identifies each projection. 

Constructor expressions


What we need in this case is a class representing the projection with constructor having parameters corresponding to all properties in the projection.
package com.blogspot.vardlokkur.domain;

import java.io.Serializable;

import javax.annotation.concurrent.Immutable;

@Immutable
public class EmployeeNameProjection implements Serializable {

private final Long employeeId;

private final String name;

public EmployeeNameProjection(Long employeeId, String name) {
super();
this.employeeId = employeeId;
this.name = name;
}

public Long getEmployeeId() {
return employeeId;
}

public String getName() {
return name;
}

}

Now we can run JPQL query:

select new com.blogspot.vardlokkur.domain.EmployeeNameProjection(employee.employeeId, employee.name) from Employee employee order by employee.name

which in turn will execute one single SQL query:

select EMPLOYEE_ID, EMPLOYEE_NAME from EMPLOYEE order by EMPLOYEE_NAME

This method has some disadvantages:
  • adding new projection properties increases number of constructor parameters
  • projection class name is included in JPQL query, which may lead to refactoring problems
Second disadvantage can be solved with Querydsl, which also gives you few more possibilities of building projections, but that will be subject of my next post :) 

To be continued ...

Few links for the dessert

  1. JPQL Constructor Expressions
  2. Result Classes (Constructor Expressions)

 




 

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:
java ,persistence

Published at DZone with permission of Michal Jastak, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}