kotlinjpa

JPA error in kotlin : Class 'Student' should have [public, protected] no-arg constructor


Does anyone know how I can solve this problem: 'Class 'Student' should have [public, protected] no-arg constructor'?

It's moaning about the relation to SchoolLesson

import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
import javax.persistence.JoinColumn
import javax.persistence.ManyToOne


@Entity
data class Student(
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    val id: Long = -1,

    @ManyToOne
    @NotNull
    @JoinColumn(name = "school_lesson_id", referencedColumnName = "id")
    val report: SchoolLesson,

)

#EDIT added SchoolLesson on request

    import javax.persistence.*
    import javax.validation.constraints.NotNull

    @Entity
    data class SchoolLesson(
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(nullable = false)
      val id: Long = -1,
    
      @NotNull
      val name: String = "",
    )

Solution

  • Never use data classes for @Entities. It causes a bunch of problems later on. Follow best practices listed here: https://jpa-buddy.com/blog/best-practices-and-common-pitfalls/.