skip to Main Content

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": "[email protected]",
    "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

2

Answers


  1. 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

    Login or Signup to reply.
  2. 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<>();
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search