2016-12-06 60 views
1

例如。我們可以在休眠中的兩個表之間有@OneToMany和@ManyToOne關係嗎?例如:

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; 

} 

public class User { 

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

} 

我使用傑克遜作爲一個JSON獲取他們碰上#1

org.springframework.http.converter.HttpMessageNotWritableException: 無法寫入內容:無限遞歸(StackOverflowError) (通過參考鏈: com.User [「defaultPortfolio」] - > com..Portfolio [「portfolioOwner」] - > com.Us呃[ 「defaultPortfolio」] - > com.Portfolio [ 「portfolioOwner」] - > com..User [ 「defaultPortfolio」] -

+1

[無限遞歸與Jackson JSON和Hibernate JPA問題]的可能重複(http://stackoverflow.com/questions/3325387/infinite-recursion-with-jackson-json-and-hibernate-jpa-issue) – BadZen

回答

1

你需要添加JSON忽視的關係前的一面:

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; 

} 
相關問題