Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

init block not called in Kotlin entities [DATAJPA-1687] #1984

Closed
spring-projects-issues opened this issue Feb 24, 2020 · 3 comments
Closed

init block not called in Kotlin entities [DATAJPA-1687] #1984

spring-projects-issues opened this issue Feb 24, 2020 · 3 comments

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Feb 24, 2020

yonigibbs opened DATAJPA-1687 and commented

When an entity is written in Kotlin and has an init block, the code in this isn't called when the entity is created by JPA (e.g. as the result of a repository query). This results in uninitialized data, and even in non-nullable Kotlin fields having a null value.

This can be seen in this GitHub repo: https://github.com/yonigibbs/spring-data-jpa-kotlin-init. This contains the following entity:

@Entity
class User(@Id val userName: String, val firstName: String, val lastName: String) {

    @Transient
    val propSetByInit: String

    init {
        propSetByInit = "foo" // In reality this would be calculated at runtime
    }
}

When this entity is constructed from a repository query, propSetByInit has a null value, even though it's a non-nullable String


Affects: 2.2.4 (Moore SR4)

Reference URL: https://github.com/yonigibbs/spring-data-jpa-kotlin-init

Attachments:

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Feb 24, 2020

Mark Paluch commented

Spring Data isn't creating entity instances. Instantiation is handled by the underlying JPA provider. Looking at the Kotlin code and the provided project, the Kotlin JPA plugin creates a default constructor that is required by JPA. The default constructor is empty and therefore the common initialization code during construction is never invoked. I attached the decompiled and the bytecode view of your User class. I recommend filing a ticket against the Kotlin plugin as clearly the plugin does not consider the init block

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Feb 24, 2020

yonigibbs commented

Ah, right, so it's down to the plugin. Gotcha. I wasn't sure where exactly in the stack was the missing logic. I'll find where to log an issue with the plugin. Thanks for such a quick response!

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Mar 2, 2020

yonigibbs commented

Just in case anyone comes across this in future, this has been logged against the Kotlin JPA plugin, and has been reproduced/confirmed by JetBrains, so hopefully it'll get sorted soon enough there.

https://youtrack.jetbrains.com/issue/KT-36944

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants