实体关系图:

java - 无法写入内容:无限递归-LMLPHP

我想接收翻译,但是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);
}

最佳答案

您建立了错误的关系,从一方面开始,您给了OneToOne,从另一个方向,您正在给OneToMany,

您的代码:

@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;
}


正确的OneToMany代码:

@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”来避免无限递归进入您的
  码

08-04 08:03