Spring数据实体:停止递归

Spring数据实体:停止递归,spring,entity-framework,recursion,spring-data,spring-rest,Spring,Entity Framework,Recursion,Spring Data,Spring Rest,我有一个包含两个实体的crudepository 机器实体 _machine_id || name || description 特征实体 characteristic_id || machine_id || name || description || type || value JSON: 问题:为什么会有递归,如何防止生成递归 机器 @Entity public class Machine { private int machine_id; private Stri

我有一个包含两个实体的
crudepository

机器实体

_machine_id || name || description
特征实体

characteristic_id || machine_id || name || description || type || value
JSON:

问题:为什么会有递归,如何防止生成递归

机器

@Entity
public class Machine {
    private int machine_id; 

    private String name;

    private String description; 

    private Set<Characteristic> characteristics;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "machine", cascade = CascadeType.ALL)
    public Set<Characteristic> getCharacteristics() {
        return characteristics; 
    }

    public void setCharacteristics(Set<Characteristic> characteristics){
        this.characteristics = characteristics;
    }

    public Machine(){}

    public Machine(String name, String description){
        this.name = name;
        this.description = description; 
    }

    @Override
    public String toString() {
        return "Machine [id=" + machine_id + ", name=" + name + ", description=" + description + "]";
    }

    @Id 
    @GeneratedValue
    public int getId() {
        return machine_id;
    }

    public void setId(int machine_id) {
        this.machine_id = machine_id;
    }

    @Column
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

将@JsonIgnore注释添加到一个连接的元素中

这些元素相互关联。json解析器尝试JSONIFI对象的所有成员。因此,如果不手动停止它,它是递归的


请看这里:

将@JsonIgnore注释添加到一个连接的元素中

这些元素相互关联。json解析器尝试JSONIFI对象的所有成员。因此,如果不手动停止它,它是递归的


请看这里:

但是,在许多实体类中,使用@JsonIgnore时,它会忽略从前端发送到引用列的数据。因此,在向多个表插入数据时,它会生成空指针错误。有没有办法解决这个问题?但是,在许多实体类中,@JsonIgnore会忽略从前端发送到引用列的数据。因此,在向多个表插入数据时,它会生成空指针错误。有没有办法解决这个问题?
@Entity
public class Machine {
    private int machine_id; 

    private String name;

    private String description; 

    private Set<Characteristic> characteristics;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "machine", cascade = CascadeType.ALL)
    public Set<Characteristic> getCharacteristics() {
        return characteristics; 
    }

    public void setCharacteristics(Set<Characteristic> characteristics){
        this.characteristics = characteristics;
    }

    public Machine(){}

    public Machine(String name, String description){
        this.name = name;
        this.description = description; 
    }

    @Override
    public String toString() {
        return "Machine [id=" + machine_id + ", name=" + name + ", description=" + description + "]";
    }

    @Id 
    @GeneratedValue
    public int getId() {
        return machine_id;
    }

    public void setId(int machine_id) {
        this.machine_id = machine_id;
    }

    @Column
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}
@Entity
public class Characteristic {
    private int characteristic_id; 

    private String name; 

    private String description; 

    private int type; 

    private int value;

    private Machine machine; 

    @ManyToOne
    @JoinColumn(name="machine_id")
    public Machine getMachine(){
        return machine;
    }

    public void setMachine(Machine machine){
        this.machine = machine;
    }

    public Characteristic() {} 

    @Id
    @GeneratedValue
    public int getId() {
        return characteristic_id;
    }

    public void setId(int characteristic_id) {
        this.characteristic_id = characteristic_id;
    }

    @Column 
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column 
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Column 
    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    @Column 
    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "Characteristic [id=" + characteristic_id + ", name=" + name + ", description=" + description + ", type=" + type
                + ", value=" + value + "]";
    }
}