2016-09-27 79 views
1

實體關係圖:無法寫入內容:無限遞歸

ERD

我想收到一個翻譯,但element將鏈接回翻譯。 所以它會崩潰,因爲它使循環..

翻譯實體:

package com.exstodigital.photofactory.model; 

import javax.persistence.*; 

import java.io.Serializable; 

/** 
* Created by youri on 21-09-16. 
*/ 
@Table(name = "translation") 
@Entity 
public class Translation implements Serializable { 
    @Id 
    @GeneratedValue 
    private int id; 

    public int getId() { 
     return id; 
    } 

    private String text; 

    public String getText() { 
     return text; 
    } 

    public void setText(String text) { 
     this.text = text; 
    } 

    @OneToOne 
    @JoinColumn(name = "language_id") 
    private Language language; 

    public Language getLanguage() { 
     return language; 
    } 

    @OneToOne 
    @JoinColumn(name = "element_id") 
    private Element element; 

    public Element getElement() { 
     return element; 
    } 

    public Translation(int id, String text) { 
     this.id = id; 
     this.text = text; 
    } 

    public Translation() { 
     // Empty constructor 
    } 

    @Override 
    public boolean equals(Object obj) { 
     return obj instanceof Translation && (!this.text.equals(((Translation) obj).text)); 
    } 
} 

元實體:

package com.exstodigital.photofactory.model; 

import javax.persistence.*; 

import java.io.Serializable; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 

/** 
* Created by youri on 21-09-16. 
*/ 
@Entity 
@Table(name = "element") 
public class Element implements Serializable { 
    @Id 
    @GeneratedValue 
    private int id; 

    public int getId() { 
     return id; 
    } 

    private String label; 

    public String getLabel() { 
     return label; 
    } 

    public void setLabel(String label) { 
     this.label = label; 
    } 

    @OneToMany 
    @JoinColumn(name = "element_id") 
    private List<Translation> translations = new ArrayList<>(); 

    public List<Translation> getTranslations() { 
     return Collections.unmodifiableList(translations); 
    } 

    public Element(int id, String label) { 
     this.id = id; 
     this.label = label; 
    } 

    public Element() { 
     // Empty constructor 
    } 

    @Override 
    public boolean equals(Object obj) { 
     return obj instanceof Element && (this.id != ((Element) obj).id || !this.label.equals(((Element) obj).label)); 
    } 
} 

TranslationDaoImpl:

@Override 
public Translation get(int id) { 
    return sessionFactory.openSession().get(Translation.class, id); 
} 
+0

的可能的複製[無限遞歸與傑克遜JSON和Hibernate JPA的問題(https://stackoverflow.com/questions/ 3325387 /無限遞歸與 - 傑克遜JSON-和休眠,JPA-問題) – Rumid

回答

1

你已經建立了錯誤的關係,從一邊你給OneToOne和相同,但F ROM第二實體你給一對多

您的代碼:

@Table(name = "translation") 
@Entity 
public class Translation implements Serializable { 

    @OneToOne 
    @JoinColumn(name = "element_id") 
    private Element element; 

} 

@Entity 
@Table(name = "element") 
public class Element implements Serializable { 

    @OneToMany 
    @JoinColumn(name = "element_id") 
    private List<Translation> translations = new ArrayList<>(); 
} 

爲OneToOne權代碼:

@Table(name = "translation") 
@Entity 
public class Translation implements Serializable { 

    @JsonIgnore  
    @OneToOne 
    @JoinColumn(name = "element_id") 
    private Element element; 

} 

@Entity 
@Table(name = "element") 
public class Element implements Serializable { 

    @OneToOne 
    (mappedBy="element") 
    private Translation translation; 
} 

爲一對多權代碼:

@Table(name = "translation") 
@Entity 
public class Translation implements Serializable { 

    @JsonIgnore 
    @ManyToOne 
    @JoinColumn(name = "element_id") 
    private Element element; 

} 

@Entity 
@Table(name = "element") 
public class Element implements Serializable { 

    @OneToMany 
    (mappedBy="element") 
    private List<Translation> translations; 
} 

使用「@JsonIgnore」,以避免無限遞歸是進入你 代碼