1

Eg.

public class Portfolio implements Serializable {

    @ManyToOne()
    @JoinColumn(name = "PORTFOLIO_OWNER", referencedColumnName = "USER_ID", foreignKey = @ForeignKey(name = "FK__USER__PORTFOLIO"), nullable = false)
    private User portfolioOwner;

    @ManyToOne()
    @JoinColumn(name = "ACCOUNT_CAPTAIN", referencedColumnName = "USER_ID", foreignKey = @ForeignKey(name = "FK__USER__PORTFOLIO2"))
    private User accountCaptain;

}

and

public class User {

    @ManyToOne
    @JoinColumn(name = "PORTFOLIO_ID", referencedColumnName = "PORTFOLIO_ID", foreignKey = @ForeignKey(name = "FK_DEF_PORTFOLIO_USER"))
    @Fetch(FetchMode.JOIN)
    private Portfolio defaultPortfolio;

}

I run into Stackoverflow on fetching them using JACKSON as a JSON

org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: Infinite recursion (StackOverflowError) (through reference chain: com.User["defaultPortfolio"]->com..Portfolio["portfolioOwner"]->com.User["defaultPortfolio"]->com.Portfolio["portfolioOwner"]->com..User["defaultPortfolio"]-

user1211
  • 1,507
  • 1
  • 18
  • 27
  • 1
    Possible duplicate of [Infinite Recursion with Jackson JSON and Hibernate JPA issue](http://stackoverflow.com/questions/3325387/infinite-recursion-with-jackson-json-and-hibernate-jpa-issue) – BadZen Dec 06 '16 at 20:43

1 Answers1

1

you need to add json ignore to one side of the relations ex:

public class Portfolio implements Serializable {

    @ManyToOne()
    @JoinColumn(name = "PORTFOLIO_OWNER", referencedColumnName = "USER_ID", foreignKey = @ForeignKey(name = "FK__USER__PORTFOLIO"), nullable = false)
    @JsonIgnore
    private User portfolioOwner;

    @ManyToOne()
    @JoinColumn(name = "ACCOUNT_CAPTAIN", referencedColumnName = "USER_ID", foreignKey = @ForeignKey(name = "FK__USER__PORTFOLIO2"))
    private User accountCaptain;

}
Bassem Salama
  • 467
  • 5
  • 15