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?