6

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?

Damien
  • 4,081
  • 12
  • 75
  • 126
  • 1
    Jackson has some annotations for managing circular references. See [JsonManagedReference](http://fasterxml.github.io/jackson-annotations/javadoc/2.5/com/fasterxml/jackson/annotation/JsonManagedReference.html) and [JsonBackReference](http://fasterxml.github.io/jackson-annotations/javadoc/2.5/com/fasterxml/jackson/annotation/JsonBackReference.html) – kuujo Mar 21 '15 at 03:21
  • Thanks kuujo - i added those and it seems to work but now my credential object is not being returned from the UserTasks object on json calls. I have updated the question with these details also. Any idea what is up here? I do not have @JsonIgnore specified – Damien Mar 21 '15 at 11:32

2 Answers2

3

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; }
}
Nikolay Rusev
  • 4,060
  • 3
  • 19
  • 29
  • Thanks Nikolay- i added those and it seems to work but now my credential object is not being returned from the UserTasks object on json calls. I have updated the question with these details also. Any idea what is up here? I do not have @JsonIgnore specified – Damien Mar 21 '15 at 11:32
  • 1
    Hello, in that case if you want `Credential` object to be serialized in UserTask you should return to `@JsonIdentityInfo`. Remove `@JsonBack` and `@JsonManagedReference` annotation and add `@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")` to your classes – Nikolay Rusev Mar 21 '15 at 16:40
2

I went with the annotation below in the end

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

Thanks for your help guys

Damien
  • 4,081
  • 12
  • 75
  • 126