Java 16 Records with JPA and jOOQ
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.
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:
Thanks to Java 16 Records this is now much simpler:
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:
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.
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 »
I know what you mean but Java Records were never meant to be used as mutable objects. So it’s a replacement for DTOs.
IMHO this is not a problem because entities should be used very sparsely and only if you really want to change the underlaying data.