Menu

Kotlin data class in Spring Boot 4: what the bytecode shows
📰

Kotlin data class in Spring Boot 4: what the bytecode shows

DEV Community: java·Dmitry Protsenko·about 11 hours ago
#22Aa7xH5
#devtojava#devto#dev#kotlin#enter
Reading 0:00
15s threshold

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 .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.