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

Simplest way to use @JsonManagedReference #294

Open
davidmoser opened this issue Jan 29, 2020 · 2 comments
Open

Simplest way to use @JsonManagedReference #294

davidmoser opened this issue Jan 29, 2020 · 2 comments

Comments

@davidmoser
Copy link

We're trying to use @JsonManagedReference but so far the @JsonBackReference is null when we deserialize.

We tried the approach from #129 .
But actually this test doesn't check that the back reference is set when deserializing. We added
assertNotNull(value.colors.single().car)
and then the test fails.

The example from #149 works, but it uses a backing field, which we'd prefer not to use.

What's the simplest way to get the example from #129 to work?

@NeonMika
Copy link

NeonMika commented May 12, 2020

I have exactly the same problem that the "solution" from #129 (which is missing an assertNotNull) does not work, since the field that should be set through @JsonBackReference is still null.

This is the solution proposed in #129 (which is not working):

data class Car(
    val id: Long,
    @JsonManagedReference
    val colors: MutableList<Color> = mutableListOf()
)

data class Color (
    val id: Long,
    val code: String) {

    @JsonBackReference
    lateinit var car: Car
}

I found out that it is working if do not only pull the @JsonBackReference out of the constructor (and make it a lateinit var), but if you also pull the @JsonManagedReference out of the constructor.
The following code is working on my machine:

data class Car(
        val id: Long) {
    @JsonManagedReference
    val colors: MutableList<Color> = mutableListOf()
}

data class Color(
        val id: Long,
        val code: String) {
    @JsonBackReference
    lateinit var car: Car
}

@BrunoRosendo
Copy link

I have exactly the same problem that the "solution" from #129 (which is missing an assertNotNull) does not work, since the field that should be set through @JsonBackReference is still null.

This is the solution proposed in #129 (which is not working):

data class Car(
    val id: Long,
    @JsonManagedReference
    val colors: MutableList<Color> = mutableListOf()
)

data class Color (
    val id: Long,
    val code: String) {

    @JsonBackReference
    lateinit var car: Car
}

I found out that it is working if do not only pull the @JsonBackReference out of the constructor (and make it a lateinit var), but if you also pull the @JsonManagedReference out of the constructor. The following code is working on my machine:

data class Car(
        val id: Long) {
    @JsonManagedReference
    val colors: MutableList<Color> = mutableListOf()
}

data class Color(
        val id: Long,
        val code: String) {
    @JsonBackReference
    lateinit var car: Car
}

A true life saver, but it'd be nice to have this working with the constructor too. I spent some considerable time debugging until I got to this thread

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

No branches or pull requests

3 participants