spring-io / initializr

A quickstart generator for Spring projects
https://start.spring.io
Apache License 2.0
3.43k stars 1.73k forks source link

Configure Kotlin's all-open plugin for JPA entities #1572

Open YangSiJun528 opened 1 week ago

YangSiJun528 commented 1 week ago

When creating a project with Kotlin and Spring Data JPA via Spring Initializr, the plugin.spring and plugin.jpa plugins are provided in build.gradle.kts or build.gradle. However, despite these plugins, the all-open configuration for JPA entities is not included by default. (The same goes for using Maven. There are differences in plugins, but all-open for JPA Entites is not supported.)

Since Kotlin classes are final by default, developers must manually add the all-open configuration for JPA entities on top of the settings provided by Spring Initializr. Otherwise, eager loading occurs, even when relationships between entities are explicitly marked as lazy. This behavior can be confusing to developers.

This concern was raised earlier in this issue, but it was closed with the suggestion to raise it with the Kotlin team. Unfortunately, the issue (KT-28594) has remained unresolved for nearly six years. As discussed in this pull request, the Kotlin team no longer provides framework-specific solutions for this.

To improve the developer experience, it would be beneficial to include a plugin that provides all-open configuration for JPA entities or automatically adds this configuration.

I’m interested in contributing to this feature and would appreciate any guidance on how to proceed with the implementation.

mhalbritter commented 5 days ago

Hello,

if I understand this correctly, it's about adding

allOpen {
    annotation("jakarta.persistence.Entity")
    annotation("jakarta.persistence.MappedSuperclass")
    annotation("jakarta.persistence.Embeddable")
}

to build.gradle and

<configuration>
// ...
        <pluginOptions>
            <option>all-open:jakarta.persistence.Entity</option>
            <option>all-open:jakarta.persistence.MappedSuperclass</option>
            <option>all-open:jakarta.persistence.Embeddable</option>
        </pluginOptions>
</configuration>

to pom.xml.

Correct?

YangSiJun528 commented 4 days ago

Yes, that's correct. In Kotlin + Spring Data JPA, the configuration must be written for Lazy Loading to work.

YangSiJun528 commented 4 days ago

I have provided a simple example project.

Project Link: https://github.com/YangSiJun528/example-kotlin-jpa

The project contains two branches:

You can open the project and run ./gradlew test to execute the tests.

YangSiJun528 commented 2 days ago

I'm interested in contributing to this feature and would like to know if it's possible for me to work on implementing it.