Avatar ·

Kotlin with JPA/Hibernate: default constructor hell

Hi everyone! I've been writing in Kotlin for about six months, and now I've run into a problem when using JPA/Hibernate. I have a simple entity:

\n\n
@Entity\ndata class User(\n    @Id @GeneratedValue\n    val id: Long = 0,\n    val name: String,\n    val email: String\n)\n
\n\n

When I run the application, Hibernate throws an exception:

\n\n
org.hibernate.InstantiationException: No default constructor for entity: com.example.User\n
\n\n

I understand that Hibernate requires a no-arg constructor to create objects via reflection. But Kotlin data classes don't have such a constructor if the parameters don't have default values. I tried adding @NoArg from Jackson, but it didn't help. I also heard about the kotlin- plugin