0

I have a CrudRepository with two entities.

machine entity

_machine_id || name || description

characteristic entity

characteristic_id || machine_id || name || description || type || value

JSON:

[{"name":"CNC","description":"Metallverarbeitung","characteristics":[{"name":"Final Piping Connections","description":"Final Piping Connections","type":0,"value":0,"machine":{"name":"Marvins Maschine","description":"Bizeps","characteristics":[{"name":"Final Piping Connections","description":"Final Piping Connections","type":0,"value":0,"machine":{"name":"Marvins Maschine","description":"Bizeps","characteristics":[{"name":"Final Piping Connections","description":"Final Piping Connections","type":0,"value":0,"machine":{"name":"Marvins Maschine","description":"Bizeps","characteristics":[{"name":"Final Piping Connections","description":"Final Piping Connections","type":0,"value":0,"machine":{"name":"Marvins Maschine","description":"Bizeps","characteristics":[{"name":"Final Piping Connections","description":"Final Piping Connections","type":0,"value":0,"machine":{"name":"Marvins Maschine","description":"Bizeps","characteristics":[{"name":"Final Piping Connections","description":"Final Piping Connections","type":0,"value":0,"machine":{"name":"Marvins Maschine","description":"Bizeps","characteristics":[{"name":"Final Piping Connections","description":"Final Piping Connections","type":0,"value":0,"machine":{"name":"Marvins Maschine","description":"Bizeps","characteristics":[{"name":"Final Piping Connections","description":"Final Piping Connections","type":0,"value":0,"machine":{"name":"Marvins Maschine","description":"Bizeps","characteristics":[{"name":"Final Piping Connections","description":"Final Piping Connections","type":0,"value":0,"machine":{"name":"Marvins Maschine","description":"Bizeps","characteristics":[{"name":"Final Piping Connections","description":"Final Piping Connections","type":0,"value":0,"machine":{"name":"Marvins Maschine","description":"Bizeps","characteristics":

Question: Why is there a recursion and how can I prevent from generating it?

Machine

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

Characteristic

@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 + "]";
    }
}
jublikon
  • 3,427
  • 10
  • 44
  • 82

1 Answers1

4

Add @JsonIgnore Annotation to one of the joined elements.

The elements refer to each other. And the json-parser tries to jsonifiy all the members of an object. So it is recursive if you don't stop it manually.

Look also here: Infinite Recursion with Jackson JSON and Hibernate JPA issue

Community
  • 1
  • 1
Benjamin Schüller
  • 2,104
  • 1
  • 17
  • 29
  • But, with @JsonIgnore, it ignores the data sent to the referencing column from the frontend, in Many-Entity class. So, it generates null pointer errors when inserting data to the Many-table. Is there a way to fix this issue? – Udith Indrakantha Jul 27 '19 at 20:22