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>();
1
  • 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. Commented Sep 3, 2019 at 11:34

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

-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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.