Core Java

Java 16 Records with JPA and jOOQ

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.

1
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:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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:

1
2
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:

1
2
3
4
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

Published on Java Code Geeks with permission by Simon Martinelli, partner at our JCG program. See the original article here: Java 16 Records with JPA and jOOQ

Opinions expressed by Java Code Geeks contributors are their own.

Simon Martinelli

Simon Martinelli is a passionate Java, performance optimization and application integration expert and an active member of the Java community process (JSR-352 Java Batch and JSR-354 Money and Currency API). He is the owner of 72 Services LLC and an adjunct professor at Berne University of Applied Science in Switzerland, teaching software architecture and design and persistence technologies
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Tom SIlverman
Tom SIlverman
3 years ago

Thanks for the nice demo. I’ve had JPA entities on my mind since the first glance at the Java 16 new ‘record’ feature. I was pondering about how an immutable data structure could be utilized with ORM’s. Was there some new sort of magic allowing Entity classes to become records? How do we get around the getters and setters method names conventions that are so deeply rooted into Jaa ORM’s? No! Unfortunately, as I see it in this demo there’s no new magic when it comes to JPA. Not without reverting back to the old DTO pattern, which requires a… Read more »

Back to top button