1

I have problem with hibernate and JSON, when i use many to many for the same class, i get recursion

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.util.Set;
import java.util.UUID;

@Entity
public class AssistantGraphVertex {

    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    private UUID uuid;

    @ManyToOne
    private AssistantGraphs assistantGraphs;

    private String name;
    private String description;

    @ManyToMany(cascade = {CascadeType.ALL})
    @JoinTable(name = "assistant_graph_edge",
            joinColumns = { @JoinColumn(name = "parent_uuid") },
            inverseJoinColumns = { @JoinColumn(name = "child_uuid") }
    )
    private Set<AssistantGraphVertex> parents;

    @ManyToMany(mappedBy = "parents")
    private Set<AssistantGraphVertex> children;

    public UUID getUuid() {
        return uuid;
    }

    public void setUuid(UUID uuid) {
        this.uuid = uuid;
    }

    public AssistantGraphs getAssistantGraphs() {
        return assistantGraphs;
    }

    public void setAssistantGraphs(AssistantGraphs assistantGraphs) {
        this.assistantGraphs = assistantGraphs;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Set<AssistantGraphVertex> getChildren() {
        return children;
    }

    public void setChildren(Set<AssistantGraphVertex> children) {
        this.children = children;
    }

    public Set<AssistantGraphVertex> getParents() {
        return parents;
    }

    public void setParents(Set<AssistantGraphVertex> parents) {
        this.parents = parents;
    }
}

Problem look like this:

Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError);

I would like to receive information only for one class about its parents and children, but for the next nested class, I do not need this information, how can this be better achieved?

  • is this a duplicate of https://stackoverflow.com/questions/3325387/infinite-recursion-with-jackson-json-and-hibernate-jpa-issue?rq=1 ?? – Jack Flamp Sep 04 '17 at 12:50
  • @JackFlamp no man, i not want ignore this json fild, read please question, i need info about children and parents – Andrei Sergeev Sep 04 '17 at 12:56

1 Answers1

6

you can simply ignore serializing one side suing @JsonIgnore

@JsonIgnore
@ManyToMany(cascade = {CascadeType.ALL})
@JoinTable(name = "assistant_graph_edge",
        joinColumns = { @JoinColumn(name = "parent_uuid") },
        inverseJoinColumns = { @JoinColumn(name = "child_uuid") }
)
private Set<AssistantGraphVertex> parents;

or

@JsonIgnore
@ManyToMany(mappedBy = "parents")
private Set<AssistantGraphVertex> children;

Or you can using @JsonIdentityInfo which will create a reference for hibernate in the second level of serialization which will break the cycle

@Entity
@JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="@id")
public class AssistantGraphVertex {

For more details check this link

Amer Qarabsa
  • 6,412
  • 3
  • 20
  • 43