question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deserialize JsonManagedReference on many to many relations

See original GitHub issue

Jackson Version: 2.9.7

Objects with a ManyToMany relationship which is managed by @JsonManagedReference/@JsonBackReference cannot be deserialized. This is easiest to show with an example.

public class JsonReferenceTest {
    class Customer {
        @JsonManagedReference("users")
        public Collection<User> users = new ArrayList<>();

        public String name = "company";
    }

    class User {
        @JsonBackReference("users")
        public Collection<Customer> customers = new ArrayList<>();

        public String name = "user";
    }

    ObjectMapper objectMapper = new ObjectMapper();

    @Test
    public void testDeserialize() throws IOException {
        String customer = "{\"name\":\"asdf\"}";

        objectMapper.readValue(customer, Customer.class);
    }
}

Running the following test results in:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot handle managed/back reference 'users': back reference type (java.util.Collection) not compatible with managed type (com.widewail.JsonReferenceTest$Customer)
 at [Source: (String)"{"name":"asdf"}"; line: 1, column: 1]

Digging into BeanDeserializerBase it looks like when checking that the back reference type matches the reference type it is not checking the contained type of the collection on the back reference side.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:12
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
vignesh25nithincommented, Oct 3, 2019

Am also facing the same issue.

3reactions
EmielSitocommented, Aug 5, 2019

I’m looking for a solution but see no one has posted one

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deserialize JsonManagedReference on many to many relations
Objects with a ManyToMany relationship which is managed by @JsonManagedReference/@JsonBackReference cannot be deserialized.
Read more >
Jackson - Bidirectional Relationships - Baeldung
In the following example, we have two entities, “User” and “Item,” with a simple one-to-many relationship: The “User” entity:
Read more >
Jackson Bidirectional Relationships - 2022
There exists a one-to-many relationship between Province and City, and a one-to-one relationship between City and Province. Province.java ...
Read more >
[Solved]-Deserialize JsonManagedReference on many to ...
Annotation used to indicate that associated property is part of two-way linkage between fields; and that its role is "child" (or "back") link....
Read more >
Jackson – Bidirectional Relationships - Baeldung
@JsonManagedReference is the forward part of reference – the one that gets ... Finally, let's deserialize the entities with bidirectional relationship using ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found