Spring Data Jpa Sping Boot parent child自引用关系将子记录返回为null

bf1o4zei  于 2024-01-09  发布在  Spring
关注(0)|答案(1)|浏览(228)

我正在使用Sping Boot 和Spring Data Jpa处理一个Rest API端点。我面临的问题是子记录为null。以下是关于我接收的数据的代码和截图。
首先,返回数据的截图。Json returned Data
以下是我的模型“文章”

  1. @Entity
  2. //@JsonInclude(JsonInclude.Include.NON_NULL)
  3. @JsonIgnoreProperties({"hibernate_lazy_initializer", "handler"})
  4. @EqualsAndHashCode(onlyExplicitlyIncluded = true)
  5. @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
  6. public class Article {
  7. @Id
  8. @Getter
  9. @Setter
  10. @EqualsAndHashCode.Include
  11. private Long id;
  12. @Getter
  13. @Setter
  14. @EqualsAndHashCode.Include
  15. private Long parent_id;
  16. @Getter
  17. @Setter
  18. @EqualsAndHashCode.Include
  19. private String name;
  20. @ManyToOne(fetch = FetchType.LAZY)
  21. @Getter
  22. @Setter
  23. private Article article;
  24. @OneToMany(fetch = FetchType.LAZY, mappedBy = "article")
  25. @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
  26. @Setter
  27. private Set<Article> subArticles;
  28. @JsonIgnore
  29. public Set<Article> subArticles() {
  30. return subArticles;
  31. }
  32. }

字符串
以下是我的DTO

  1. @Data
  2. @Builder
  3. //@JsonInclude(JsonInclude.Include.NON_NULL)
  4. @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
  5. @JsonIgnoreProperties({"hibernate_lazy_initializer", "handler"})
  6. public class ArticleDTO {
  7. private Long id;
  8. private String name;
  9. private Long parent_id;
  10. private Article article;
  11. private Set<Article> subArticle;
  12. }


以下是我的控制器:

  1. @RestController
  2. @RequestMapping("/api/v1/article")
  3. public class ArticleController {
  4. @Autowired
  5. private ArticleRepository articleRepository;
  6. @GetMapping("/{id}")
  7. public ResponseEntity<ArticleDTO> getAllArticles(@PathVariable("id") Long id) {
  8. // here first show simple result/
  9. return articleRepository
  10. .findById(id)
  11. .map(mapToArticleDTO)
  12. .map(ResponseEntity::ok)
  13. .orElse(ResponseEntity.notFound().build());
  14. }
  15. @GetMapping("/{id}/siblings")
  16. public ResponseEntity<Set<ArticleDTO>> getAllSiblings(@PathVariable("id") Long id) {
  17. return articleRepository.findById(id).map(findSubArticles).map(ResponseEntity::ok)
  18. .orElse(ResponseEntity.notFound().build());
  19. }
  20. private Function<Article, ArticleDTO> mapToArticleDTO = a -> ArticleDTO.builder()
  21. .id(a.getId())
  22. .name(a.getName())
  23. .parent_id(a.getParent_id())
  24. .build();
  25. private Function<Article, Set<ArticleDTO>> findSubArticles = article -> article.getArticle().subArticles().stream()
  26. .map(a -> ArticleDTO.builder().id(a.getId()).name(a.getName()).build()).collect(Collectors.toSet());
  27. }


以下是我的Repository:

  1. @Repository
  2. public interface ArticleRepository extends JpaRepository<Article, Long>{
  3. }


以下是我的数据库记录截图Database records
任何指导,我去错误的地方将不胜感激。

mdfafbf1

mdfafbf11#

总之,Lazy fetching是在transaction下工作的,用@Transactional标记你的类。

相关问题