-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
In Kotlin, @DocumentReference(lazy = true) proxies are not automatically unwrapped #4483
Comments
I'm not quite sure there is something we can do here. When Kotlin defaults to field access, then there isn't something we can do. |
Are you suspecting that in this case the Kotlin compiler is optimizing out the property accessors and doing direct field access? I don't think that's the case. I can add this Java test to my Kotlin project, use the getters generated by Kotlin, and it still produces the same result as the Kotlin code above:
Also, I haven't had any issues using JPA lazy loading with Kotlin, so I wouldn't think that there is a fundamental limitation. In case it helps, here is the relevant bit of bytecode for
And here is the relevant bytecode from
|
I found the problem by comparing the Kotlin bytecode with the Java bytecode. Kotlin generates setters and getters as final. I assume this is breaking the proxy mechanism. To use lazy loading proxies, the Kotlin properties in the referenced document need to be declared as
This issue can be closed. |
This can be solved more generally with the
|
When using @DocumentReference(lazy = true) in Kotlin, proxies do not automatically unwrap. This makes lazy loaded documents difficult to work with. For example, if you return a document over a REST endpoint, serializing to JSON, the referenced document properties are all serialized as null. If you want to work with a lazy loaded document in Java code, you must first cast it as a LazyLoadedProxy and access
.target
to manually obtain the unwrapped object.Here is a minimal setup that reproduces the problem in a Spring Boot application:
build.gradle.kts
Person.kt
Address.kt
PersonRepository.kt
DemoApplicationTests.kt
The text was updated successfully, but these errors were encountered: