杰克逊循环依赖

时间:2015-03-21 00:19:57

标签: java json spring jackson circular-dependency

我有一个循环依赖,我现在正努力解决 拿这两个类 - 为演示目的移除锅炉板代码

第1类

@Entity
@Table(name = "T_CREDENTIAL")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
public class Credential implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;


    //Removed @JsonIgnore as want to disable certain fields if no credentials are available    
    //Also fetch this in an eager fashion instead of lazily loading it
    @OneToMany(mappedBy = "credential",fetch=FetchType.EAGER)
    private Set<UserTask> userTasks = new HashSet<>();

    ....

    .....

第2类

@Entity
@Table(name = "T_USERTASK")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
public class UserTask implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -8179545669754377924L;


    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;


    @ManyToOne(fetch=FetchType.EAGER)
    @NotNull(message = "Credential must not be null")
    private Credential credential;

不幸的是,我有一些用例,其中UserTask需要加载凭证和Credential需要加载Usertasks的案例

注释@JsonIdentityInfo似乎正在完成它的工作 如果我加载所有UserTasks,它会加载第一个userTask及其凭据,但随后该凭据对象还将加载分配给该凭证的任何UserTasks。然后会遇到新的@Id或userTask,它现在用凭证加载它而不是2个用户任务

我可以采取哪些措施来解决这个问题?

干杯 达明

- 问题更新 我现在更新了我的代码,以使用其他用户提到的新注释

第1类

    @Entity
@Table(name = "T_CREDENTIAL")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Credential implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -8696943454186195541L;



    //Removed @JsonIgnore as want to disable certain fields if no credentials are available    
    //Also fetch this in an eager fashion instead of lazily loading it
    @OneToMany(mappedBy = "credential",fetch=FetchType.EAGER)
    @JsonManagedReference("credential")
    private Set<UserTask> userTasks = new HashSet<>();
    ....
....

第2类

@Entity
@Table(name = "T_USERTASK")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class UserTask implements Serializable {


    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @ManyToOne(fetch=FetchType.EAGER)
    @NotNull(message = "Credential must not be null")
    @JsonBackReference("credential")
    private Credential credential;
....
....

现在这些类现在可以工作并且没有循环依赖 但是,当我现在进行查询以获取我的usertasks或单个任务时,即使我没有指定@jsonIgnore注释,也不会返回凭证对象。是什么原因引起了这个?

2 个答案:

答案 0 :(得分:3)

如果你使用Jackson HttpMessageConverter,你应该查看他们的文档 - http://wiki.fasterxml.com/JacksonFeatureBiDirReferences

您应该添加此处显示的注释:

public class NodeList
{
    @JsonManagedReference
    public List<NodeForList> nodes;
}

public class NodeForList
{
    public String name;

    @JsonBackReference public NodeList parent;

    public NodeForList() { this(null); }
    public NodeForList(String n) { name = n; }
}

答案 1 :(得分:1)

我最后使用了下面的注释

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")

感谢您的帮助