Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not handle managed/back reference 'defaultReference': no back reference property found

I have two model classes. One is

@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 is

@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 on save I'm getting 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]]
like image 809
Er KK Chopra Avatar asked May 21 '16 16:05

Er KK Chopra


3 Answers

The exception you are facing stems from the MappingJackson2HttpMessageConverter.

To fix it swap the annotations so you get this :

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

public class UserToolsHistory {
....    
@JsonManagedReference
private UserTools userTools; 
----
}

This tutorial explains how it must be done : jackson-bidirectional-relationships-and-infinite-recursion

like image 167
Stefan Isele - prefabware.com Avatar answered Oct 30 '22 00:10

Stefan Isele - prefabware.com


@JsonManagedReference and @JsonBackReference and replacing it with @JsonIdentityInfo

like image 24
New Wave Avatar answered Oct 30 '22 00:10

New Wave


In order to make troubleshooting easier, you can add a different name to each @JsonManagedReference and @JsonBackReference, for example:

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

This way the error is more meaningful because it prints the reference name instead of 'defaultReference'.

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

like image 40
Michal Avatar answered Oct 29 '22 23:10

Michal