Unable to process managed / backlink 'defaultReference': backlink property not found

I have two model classes. One of them -

@Entity(name = "userTools") @Table(uniqueConstraints = @UniqueConstraint(columnNames = { "assignToUser_id","toolsType_id" })) @Inheritance(strategy = InheritanceType.JOINED) @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "className") @JsonIgnoreProperties(ignoreUnknown = true) public class UserTools { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @OneToOne private ToolsType toolsType; @OneToMany(mappedBy = "userTools", fetch = FetchType.EAGER, cascade = { CascadeType.ALL }, orphanRemoval = true) @Cascade(org.hibernate.annotations.CascadeType.DELETE) @JsonManagedReference private List<UserToolsHistory> userToolsHistory; } 

and the second -

 @Entity(name = "userToolsHistory") @JsonIgnoreProperties(ignoreUnknown = true) public class UserToolsHistory { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @OneToOne private ToolsType toolsType; @ManyToOne @JsonIgnore @JsonBackReference private UserTools userTools; private String comments; } 

But when saving, I get this error:

 Can not handle managed/back reference 'defaultReference': no back reference property found from type [collection type; class java.util.List, contains [simple type, class com.dw.model.tools.UserToolsHistory]] 
+5
source share
3 answers

The exception you encountered is related to MappingJackson2HttpMessageConverter.

To fix this, replace annotations to get the following:

 public class UserTools { ... @JsonBackReference private List<UserToolsHistory> userToolsHistory; ... } public class UserToolsHistory { .... @JsonManagedReference private UserTools userTools; ---- } 

This guide explains how this should be done: jackson-bidirectional-relationships-and-infinite-recursion

+5
source

To simplify troubleshooting, you can add a different name for each @JsonManagedReference and @JsonBackReference , for example:

 @JsonManagedReference(value="userToolsHistory") private List<UserToolsHistory> userToolsHistory; 

Thus, the error is more significant because it prints the name of the link instead of "defaultReference".

Please indicate which network you are trying to serialize - UserTools or UserToolsHistory ? In any case, you can try adding getters and setters to your objects, and then add @JsonIgnore to "get {Parent} ()" in the "child" class.

+4
source

@JsonManagedReference and @JsonBackReference and replacing it with @JsonIdentityInfo

+1
source

All Articles