A Kotlin data class turns a Spring Boot DTO into a few lines, and the same few lines quietly break your JPA entity.
I compiled each class below with and without the Kotlin compiler plugins and read the bytecode and the Spring and Hibernate code that loads them. I reproduced every problem below on Spring Boot 4.1.1 (Hibernate 7.4.5, Jackson 3.1.5) and Kotlin 2.3.21 on Java 21.
What a data class becomes in Java
data class Customer ( var id : Long ? = null , var name : String )
Enter fullscreen mode
Exit fullscreen mode
Decompiled and trimmed, that’s this Java class:
public final class Customer { private Long id ; private String name ;
public Customer ( Long id , String name ) { Intrinsics . checkNotNullParameter ( name , "name" ); this . id = id ; this .…