java 无法处理托管/反向引用“defaultReference”:未找到反向引用属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37365556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:26:00  来源:igfitidea点击:

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

javajsonspringrest

提问by Er KK Chopra

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]]

回答by Stefan Isele - prefabware.com

The exception you are facing stems from the MappingHymanson2HttpMessageConverter.

您面临的异常源于 MappingHymanson2HttpMessageConverter。

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 : Hymanson-bidirectional-relationships-and-infinite-recursion

本教程解释了它必须如何完成:Hymanson-bidirectional-relationships-and-infinite-recursion

回答by Michal

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

为了使故障排除更容易,您可以为每个@JsonManagedReference和添加不同的名称@JsonBackReference,例如:

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

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

这样错误更有意义,因为它打印引用名称而不是“defaultReference”。

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

请指出您要序列化哪个实体 -UserTools还是UserToolsHistory?无论如何,您可以尝试向实体添加 getter 和 setter,然后添加@JsonIgnore到“子”类中的“get{Parent}()”。

回答by New Wave

@JsonManagedReferenceand @JsonBackReferenceand replacing it with @JsonIdentityInfo

@JsonManagedReference并将@JsonBackReference其替换为@JsonIdentityInfo