我有两个模型类。一个是
@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;
}
第二个是
@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;
}
但是在保存时我收到了这个错误:
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]]
最佳答案
为了方便排查问题,可以为@JsonManagedReference
和@JsonBackReference
分别添加不同的名称,例如:
@JsonManagedReference(value="userToolsHistory")
private List<UserToolsHistory> userToolsHistory;
这样错误更有意义,因为它打印引用名称而不是“defaultReference”。
请指出您要序列化哪个实体 - UserTools
或 UserToolsHistory
?无论如何,您可以尝试向您的实体添加 getter 和 setter,然后将 @JsonIgnore
添加到“child”类中的“get{Parent}()”。
关于java - 无法处理托管/反向引用 'defaultReference' : no back reference property found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37365556/