Lombok - java.lang.StackOverflowError: null on toString method

后端 未结 2 1674
遥遥无期
遥遥无期 2021-02-19 14:47

I have two classes Product and Categorie. When I would like to modify the list of products in categorie with categoryRepository.save(c1) a

2条回答
  •  情话喂你
    2021-02-19 15:07

    You are having a circular reference in the toString method generated by Lombok.

    • Product is referencing Categorie on toString, which is referencing Product, and so on

    You could use the exclude a property @ToString, but it is going to be deprecated soon, so use the @ToString.Exclude:

    @Document
    @Data @AllArgsConstructor @NoArgsConstructor @ToString
    public class Product {
      ...
    
      @ToString.Exclude
      private Categorie categorie;
    
      ...
    }
    
    @Document
    @Data @AllArgsConstructor @NoArgsConstructor @ToString
    public class Categorie {
      ...
    
      @ToString.Exclude
      private Collection products=new ArrayList<>();
    
      ...
    }
    

    Lombok refs here and here

提交回复
热议问题