0

I have an entity called investimento which needs to be self-joined since it has more investimento inside of it. I mapped it by following many guides here but when i execute a findAll with my service it just goes looping trying to execute itself over and over. What can i do to avoid this? The other @ManyToOne are just other tables with simple columns in them. How can i fix this please?

@Entity

@Table(name="investimento") public class Investimento implements Serializable {

private static final long serialVersionUID = 8883940320251385456L;

@Id
@GeneratedValue
@Column(name="id", nullable=false)
private Long id;

@Column(name="codice", nullable = false, unique = true)
private String codice;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="codice_padre", referencedColumnName = "codice")
private Investimento investimentoPadre;

@OneToMany(mappedBy = "investimentoPadre",fetch = FetchType.LAZY)
private Set<Investimento> duplicati = new HashSet<Investimento>();
  • Typically occurs when you are serializing to JSON or have created a circular dependency in `hashcode()` or `equals()` methods (possibly using Lombok). Provide more context. Add the stack trace of the exception. – Alan Hay Sep 03 '19 at 11:34

2 Answers2

0

Finally fixed it, turns out i needed @JsonManagedReference because Jackson tries to serialise both ends of the relationship and ends up in a recursion.

Found it also here : Infinite Recursion with Jackson JSON and Hibernate JPA issue

-1

Check your data. I think there is a loop in your data. Say you have three Investimento rows, A, B and C and you try to fetch A with Hibernate. A references B, so Hibernate fetches B. B references C so Hibernate fetches C. C references A so it will try to fetch A. There you are, a loop. You can prevent hibernate from joining by setting hibernate.max_fetch_depth to 0, but this does not apply to the OneToMany