I wanted to simplify my question. I have a parent and a child entity.
public class Parent {
private String name;
@OneToMany
private List<Child> childs;
}
and
public class Child {
private String name;
@ManyToOne
private Parent parent;
}
What I want to do is when I serialize these entities, it should like this.
{
"parentList": [
{
"id": "1",
"name": "parent",
"childs": [
{
"id": "1",
"name": "chiled",
"parent" : "1"
}
],
}
]
}
and
{
"childList": [
{
"id": "1",
"name": "child",
"parent": [
{
"id": "1",
"name": "parent",
"childs": [
{
"id": "1",
}
}
],
}
]
}
Like this:
Parent -> Child -> parentID
Child -> Parent -> childID
If I update my entities like as you can see below.
public class Parent {
private String name;
@JsonIdentityReference(alwaysAsId = true)
@OneToMany
private List<Child> childs;
}
and
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Child {
private String name;
@JsonIdentityReference(alwaysAsId = true)
@ManyToOne
private Parent parent;
}
With this setup I get almost what I want.
Child -> Parent -> ChildID (good)
Parent -> ChildID (bad)
I need Parent to have whole Child entity and that Child entity should only have parentID. If I add @JsonIdentityInfo
to both entities, then result looks like this:
Child -> parentID (bad)
Parent -> childID (bad)
If I remove @JsonIdentityInfo
from both, then I have recursive problem again.
Don't know how to get what I want.