Deserialize JsonManagedReference on many to many relations
See original GitHub issueJackson 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:
- Created 5 years ago
- Reactions:12
- Comments:5 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Am also facing the same issue.
I’m looking for a solution but see no one has posted one