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 Spring Data MongoDB: @DBRef does not handle Back-References -> Stackoverflow Exception [DATAMONGO-2299] #3156 Spring Data MongoDB: @DBRef does not handle Back-References -> Stackoverflow Exception [DATAMONGO-2299] #3156 spring-projects-issues opened this issue Jun 11, 2019 · 4 comments

bollywood-coder opened DATAMONGO-2299 and commented

As referenced to the Spring Github Issue page.

spring-projects/spring-boot#17105

Affects: 2.1.8 (Lovelace SR8)

Reference URL: spring-projects/spring-boot#17105

Christoph Strobl commented

In order to read this type of cyclic references Spring Data MongoDB requires the @DBRef to be lazy . Due to constructor lookup the Child argument is required to create the Parent object. Now, creating the Child instance with the dbref to a Parent requires the referenced Parent object to be fully initialized in order to make the internal cache lookup possible. And exactly that is not the case.

The following would work though it is definitely not idiomatic Koltin

open class Parent(var id: String, val child: Child) {
    init { child.parent = this }
class Child {
    @DBRef(lazy=true)
    lateinit var parent: Parent

Hi Christoph,

thanks for taking care about this problem.

Yes your solution works but as you mentioned it not really idiomatic Kotlin.
I have created two more solutions on Stackoverflow: https://stackoverflow.com/questions/56542330/kotlin-mongodb-and-dbref-back-reference-stackoverflow-exception/56558981#56558981.

However, I am not totally happy with all of them. Is there not any way that Spring Data Mongo can manage that problem natively?

It seems like Jackson Mapper has no problem with cyclic references. Do they do something different?

status: feedback-reminder We've sent a reminder that we need additional information before we can continue label Jan 6, 2021