1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

KotlinでInstantiationException: No default constructor for entityになった時の解決策

Posted at
Exceptionが発生するコード
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id

@Entity
data class Car (var brand: String,
                var model: String,
                var color: String,
                var registerNumber: String,
                var year: Int,
                var price: Int,
                @Id
                @GeneratedValue(strategy = GenerationType.AUTO)
                var id: Long)

デフォルト値を設定しないとエラーが発生するとのことなので、各フィールドにデフォルト値を設定したらエラーが解消した。

Exceptionが発生しないコード
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id

@Entity
data class Car (var brand: String = "",
                var model: String = "",
                var color: String = "",
                var registerNumber: String = "",
                var year: Int = 0,
                var price: Int = 0,
                @Id
                @GeneratedValue(strategy = GenerationType.AUTO)
                var id: Long = -1)

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?