Jackson 循环依赖项

2023-11-23

我有一个循环依赖,我现在正在努力解决它 学习这两个课程 - 出于演示目的删除了样板代码

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 的用例

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

我有什么想法可以避免这个问题吗?

干杯 达米安

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

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

这些类现在可以工作并且没有循环依赖关系 但是,当我现在执行查询来获取用户任务或单个任务时,即使我没有指定 @jsonIgnore 注释,也不会返回凭证对象。任何想法是什么导致了这个?


如果您使用 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; }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Jackson 循环依赖项 的相关文章

随机推荐