首页 > 解决方案 > 使用@JsonIdentityInfo 或@JsonManagedReference,@JsonBackReference 会导致我在类中的属性在解析时被忽略

问题描述

我一直有休眠循环依赖问题,这是下面的类:

   @Entity
    @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="id")
    public class Task extends DefaultEntity{


        @ManyToOne
        @JoinColumn(name = "cloth_model_id")
        //@JsonIgnore
        private ClothModel clothModel;

        private String details;

        private boolean completed;

        @ManyToOne
        @JoinColumn(name = "employee_id")
        private Employee employee;
}




   @Entity
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="id")
public class ClothModel extends DefaultEntity {

    private ClothingType clothingType;

    private int quantity;

    private double amount;

    @Transient
    private String modelStructure;

    private String savedUrl;


    @Transient
    //@JsonManagedReference
    private List<Task> tasks = new ArrayList<>();
}



 @MappedSuperclass
public class DefaultEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(columnDefinition = "serial")
    private long id;

    @Column(name = "Time",columnDefinition= "TIMESTAMP WITH TIME ZONE")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateCreated;

我删除了 getter 和 setter,所以代码不会太多

当我将@JsonIdentityInfo 或@JsonManagedReference、@JsonBackReference 应用于 ClothModel.class 和 Task.class 时,我注意到当我尝试向其写入 JSON 时,Task.class 中的clothModel 属性将被忽略,它的行为就像我正在使用@Jsonignore 注释。为什么会这样,为什么没有针对 Hibernate 循环递归的适当 100% 工作修复。

标签: javahibernatespring-bootjackson-databind

解决方案


推荐阅读