杰克逊循环依赖 [英] Jackson Circular Dependencies

查看:265
本文介绍了杰克逊循环依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a circular dependency that I am struggling to solve right now Take these two classes - boiler plate code removed for demo purposes

Class 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<>();

    ....

    .....

Class 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的情况

Unfortunately I have use cases where UserTask needs to load the credentials and cases where Credential needs to load the Usertasks

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

The annotation @JsonIdentityInfo seems to be doing its job If i load all UserTasks, it loads the first userTask and its credential, but then that credential object will also load any UserTasks that are assigned to that credential. This then encounters the new @Id or a userTask which now loads it with the credential instead of as 2 users tasks

任何想法我可以做些什么来规避这个问题?

Any ideas what I can do to circumvent this problem?

干杯
Damien

Cheers Damien

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

--Question Update I have updated my code now to use the new annotations as mentioned by other users

Class 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<>();
    ....
....

Class 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注释,也不会返回凭证对象。是什么导致了这个?

These classes now now work and have no circular dependencies However, when i do a query now to get my usertasks or a single task, the credential object is not returned even though I do not have the @jsonIgnore annotation specified. Any ideas what is causing this?

推荐答案

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

if you are using Jackson HttpMessageConverter, you should check their documentation - http://wiki.fasterxml.com/JacksonFeatureBiDirReferences

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

You should add annotations shown here:

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; }
}

这篇关于杰克逊循环依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆