1
實體關係圖:無法寫入內容:無限遞歸
我想收到一個翻譯,但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);
}
的可能的複製[無限遞歸與傑克遜JSON和Hibernate JPA的問題(https://stackoverflow.com/questions/ 3325387 /無限遞歸與 - 傑克遜JSON-和休眠,JPA-問題) – Rumid