I am developing with Kotlin and JPA recently. I use Kotlin data class as JPA @Entity class.

But now, There comes some problem with relation

@ManyToOne(fetch = FetchType.LAZY, optional = true)

懒惰获取不适用于数据类.

I have learned that Kotlin data class is default to be 'final' so that Hibernate can't generate proxy for them.

I wonder is this a mistake to use Kotlin data class as JPA @Entity class or there is other ways to make the lazy fetching work properly with data class.

推荐答案

This Spring official guides表明我们不应该将Kotlin数据类与Spring-Data-JPA一起使用.

他们说:

Here we don’t use data classes with val properties because JPA is not designed to work with immutable classes or the methods generated automatically by data classes. If you are using other Spring Data flavor, most of them are designed to support such constructs so you should use classes like data class User(val login: String, …​) when using Spring Data MongoDB, Spring Data JDBC, etc.

Kotlin相关问答推荐

UByte范围. Min_UTE.. UByte.MAX_UTE不符合预期

在Webflux应用程序中通过kotlin协程启动fire and forget job

数据流弹性模板失败,出现错误&未知非复合转换urn";

用于将 0.5 变为 0 的 round() 函数的模拟

如何从 Period.between() 返回的字符串中提取信息? (Kotlin )

如何将 `when` 与 2 个密封类一起使用并获取内部值?

如何从 var list 或可变列表中获取列表流

Swift vs Kotlin 在排序数组上的表现

如何从不同的功能发出流量值? Kotlin 协程

kotlin 扩展属性的惰性初始化器中的这个引用

有没有办法在spring webflux和spring data react中实现分页

API 'variant.getJavaCompile()' 已过时

如何处理 Kotlin 中的异常?

使用 clear() 删除 EncryptedSharedPreferences 不起作用

在调用Kotlin数据类中的超类构造函数之前访问函数

Kotlin:使用Gradle进行增量编译

如何在Kotlin中创建填充空值的通用数组?

Kotlin val difference getter override vs assignment

编译错误:-Xcoroutines has no effect: coroutines are enabled anyway in 1.3 and beyond

Kotlin for assertThat(foo, instanceOf(Bar.class))