Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson Circular Dependencies

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;

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

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?

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

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?

like image 204
Damien Avatar asked Mar 21 '15 00:03

Damien


2 Answers

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; }
}
like image 66
Nikolay Rusev Avatar answered Sep 19 '22 12:09

Nikolay Rusev


I went with the annotation below in the end

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

Thanks for your help guys

like image 43
Damien Avatar answered Sep 18 '22 12:09

Damien