使用@JsonIdentityInfo或@ JsonManagedReference,@ JsonBackReference会使我的类中的属性在解析到该类时被忽略

时间:2018-10-04 12:28:27

标签: java hibernate spring-boot jackson-databind

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

   @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,所以代码不会太多

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

0 个答案:

没有答案