Java 16 Records with JPA and jOOQ

The new Java version 16 includes a new feature: Records

blog-post-img

Java Records

The new Java version 16 includes a new feature: Records

https://openjdk.java.net/jeps/395 “Enhance the Java programming language with records, which are classes that act as transparent carriers for immutable data. Records can be thought of as nominal tuples.”

Let’s try Java records with JPA and jOOQ.

JPA Constructor Expression

One way to use projection in JPA queries is using the constructor expression. The name constructor expression implies that the constructor is called with the fields from the projection.


select new com.demo.dto.EmployeeDTO(e.name, e.department.name) from Employee e
    

In the example we have a DTO called EmployeeDTO and the constructor takes two Strings as parameters.

With Java before Java 16 we would create a class like this:


public final class EmployeeDTO {

    private final String employeeName;
    private final String departmentName;

    public EmployeeDTO(String employeeName, String departmentName) {
        this.employeeName = employeeName;
        this.departmentName = departmentName;
    }

    public String employeeName() {
        return employeeName;
    }

    public String departmentName() {
        return departmentName;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) return true;
        if (obj == null || obj.getClass() != this.getClass()) return false;
        var that = (EmployeeDTO) obj;
        return Objects.equals(this.employeeName, that.employeeName) &&
                Objects.equals(this.departmentName, that.departmentName);
    }

    @Override
    public int hashCode() {
        return Objects.hash(employeeName, departmentName);
    }

    @Override
    public String toString() {
        return "EmployeeDTO[" +
                "employeeName=" + employeeName + ", " +
                "departmentName=" + departmentName + ']';
    }
}
   

Thanks to Java 16 Records this is now much simpler:


public record EmployeeDTO(String employeeName, String departmentName) {
}
   

This Record will contain the required constructor and also the methods to get the employeeName and the departmentName so it’s a perfect fit for JPAs constructor expression!

jOOQ SQL Projection

Beside JPA there is another great solution for accessing relational database systems: jOOQ

With jOOQ we can write type-safe SQL in Java. And very often we also want DTOs as a result. Also here Java Records shine:


List<EmployeeDTO> employees = dsl
   .select(EMPLOYEE.NAME, DEPARTMENT.NAME)
   .from(EMPLOYEE).join(DEPARTMENT).on(EMPLOYEE.DEPARTMENT_ID.eq(DEPARTMENT.ID))
   .fetchInto(EmployeeDTO.class);
    

Conclusion

Java Records are a great addition to the Java language and a great fit to use with persistence technologies like JPA or jOOQ.

If you want to try it on your own, please find the example code on GitHub: https://github.com/72services/java16-jpa-jooq