I have a spring rest application with two entities with a bidirectional relationshop (one-to-many, many to one). To overcome nested fetching issues, @JsonManagedReference/@JsonBackReference has been used for a perent/child relationship between entities.
The entites look as this:
@Entity
@Table(name = "Parent")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Parent implements java.io.Serializable {
private Integer id;
private List<Child> childList;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "ID", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
@JsonManagedReference
public List<Child> getChildList() {
return childList;
}
public void setChildListe(List<Child> childListe) {
this.childList = childList;
}
}
@Entity
@Table(name = "Child")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Child implements java.io.Serializable {
private Integer id;
private Parent parent;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "ID", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ParentID")
@JsonBackReference
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
}
This works fine when fetching the Parent element, the childset is then fetched alongside and displayed as an json-array.
However, there is no reference to parent in the child element due to the usage of jsonbackreferance.
I would like to either get the parent object attached to the child-elemens in the json response, or at least fetch the parent id, when requesting for a singlle child element through rest.
All feedback will be appriciated :)