跳至主要內容

Kotlin/Springboot/Mongodb – @DBRef(lazy=true) Cannot subclass final class error

During my recent work which requires me to work on a backend. It’s a Springboot with Kotlin backend and Database is mongodb. This backend is integrating some GCP APIs.

Since there is a need that some collections would have bidirectional relationship. In this case, it may cause another issue that’s the infinite recursion exception.

Like the post here:

https://stackoverflow.com/questions/44093228/infinite-recursion-issue-with-dbref-lazy-true

However, it’s not the issue that I want to address in this post.

The topic, I want to talk about is …

Kotlin has classes and their members final by default, which makes it inconvenient to use frameworks and libraries such as Spring AOP that require classes to be open.

https://kotlinlang.org/

In this case, we need to leverage All-open compiler plugin and makeorg.springframework.data.mongodb.core.mapping.Document to be open.

What you need to do is, you need to add some configurations in build.gradle.kts

plugins {
  id("org.jetbrains.kotlin.plugin.allopen") version "1.8.21"
}

allOpen {
 annotation("org.springframework.data.mongodb.core.mapping.Document")
}

For more information, you can refer these two links…

https://kotlinlang.org/docs/all-open-plugin.html

https://stackoverflow.com/questions/56095909/dbreflazy-true-with-a-kotlin-data-class-throwing-cannot-subclass-final-clas

分類:Issues

搶先發佈留言

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

由 Compete Themes 設計的 Author 佈景主題