1

I am working on a spring boot app with postgresql. I have three classes: User, Medcin that inherits from User, and Cabinet. In relationships, the Medcin can be in one Cabinet, and Cabinet can have one or more Medcin

@Entity
@Getter
@Setter
@NoArgsConstructor

@AllArgsConstructor
@Table(name = "_user")

public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;
    private String firstName;
    private String lastName;
    private int age;
    private String adress;
    private String cin;
    private String login;
    private String password;

}
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Medecin extends User {
    private int inp;
    private String specialite;
    @ManyToOne
    private Cabinet cabinet;}
    @Entity(name = "Cabinet")
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Cabinet {
        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE)
        @Column(name = "cabinet_id")
        private Long id;
        private String denomination;
        private String adresse;
        private int telephone;
        private double longitude;
        private double latitude;
        @OneToMany(mappedBy = "cabinet", cascade = CascadeType.ALL)
        private List<Medecin> medecins = new ArrayList<>();
    }

in the post request to create a cabinet I send in the body

{
    "denomination": "Cabinet name",
    "adresse": "Cabinet address",
    "telephone": 123456789,
    "longitude": 0.0,
    "latitude": 0.0
}

Then I create a Medcin the same way, like this:

{
    "email": "medecin@example.com",
    "password": "password",
    "firstName": "ww",
    "lastName": "Doe",
    "inp": 1234567,
    "specialite": "Cardiology",
    "cabinet": {
        "id": 1
    }
}

Then I do a get request to get the Medcin, but I got a result like this. I want it to return the Cabinet information I created before, but it's null.

{
    "id": 1,
    "firstName": "ww",
    "lastName": "Doe",
    "age": 0,
    "adress": null,
    "cin": null,
    "login": null,
    "password": "password",
    "inp": 1234567,
    "specialite": "Cardiology",
    "cabinet": {
        "id": 1,
        "denomination": null,
        "adresse": null,
        "telephone": 0,
        "longitude": 0.0,
        "latitude": 0.0,
        "medecins": null
    }
}

Then I do a get request to get cabinets, and I get the following error:

Could not write JSON: Infinite recursion (StackOverflowError)] with root cause
khelwood
  • 55,782
  • 14
  • 81
  • 108
Anas Hafidi
  • 349
  • 1
  • 5
  • 15

2 Answers2

1

As you first object instantiated is Cabinet, you should annotate medecins attribute with this tag: @JsonManagedReference

And then, the second one is Medecin, annotate cabinet attribute with this tag: @JsonBackReference

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Medecin extends User {
    private int inp;
    private String specialite;
    @ManyToOne
    @JsonBackReference
    private Cabinet cabinet;
}

    @Entity(name = "Cabinet")
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Cabinet {
        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE)
        @Column(name = "cabinet_id")
        private Long id;
        private String denomination;
        private String adresse;
        private int telephone;
        private double longitude;
        private double latitude;
        @OneToMany(mappedBy = "cabinet", cascade = CascadeType.ALL)
        @JsonManagedReference
        private List<Medecin> medecins = new ArrayList<>();
    }
I'm Weverton
  • 562
  • 4
  • 5
  • Thanks , that's fixed big part of my problem , now i am getting in cabinet request the medcin working on it , However, I am still facing an issue where I am unable to obtain the cabinet information in the medcin request , field not returned – Anas Hafidi Apr 26 '23 at 15:26
0

If @JsonIgnore annotation doesn't work for this case, lets try @JsonManagedReference and @JsonBackReference :

Cabinet:

@OneToMany(mappedBy = "cabinet", cascade = CascadeType.ALL)
@JsonManagedReference
private List<Medecin> medecins = new ArrayList<>();

Medecin:

@ManyToOne
@JsonBackReference
private Cabinet cabinet;

Also check this please: https://stackoverflow.com/a/43473926/10277150

gurkan
  • 509
  • 1
  • 4
  • 18