【问题标题】:JPA Repository findAll returns empty listJPA Repository findAll 返回空列表
【发布时间】:2019-05-06 13:05:30
【问题描述】:

我正在尝试从数据库中获取列表,而 findAll() 返回空列表。 我有多个 jpa 存储库,但只有一个不起作用。这是代码:

@Repository
public interface ProductCategoryRepository extends JpaRepository<ProductCategory,Integer> {
}

这是实体:

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ProductCategory {
    @Id
    private Integer id;
    private String name;
    private String description;

    @OneToMany(fetch = FetchType.EAGER,mappedBy = "productCategory", cascade = CascadeType.ALL)
    @JsonIgnore
    private List<Product> products = new ArrayList<>();
}

当我调用 productCategoryRepository.findAll() 时,它返回空列表,因此我在数据库中有很多条目。 感谢您的帮助!

【问题讨论】:

  • 您可以添加产品实体映射。
  • 我有。如果我在 productCategory 上方有@JsonIgnore,它会起作用
  • 显示您返回列表并尝试显示/使用它的代码段。很有可能,您没有正确填充列表。
  • @Autowired private ProductCategoryRepository productCategoryRepository; @GetMapping("/categs") List findCategs(){ return productCategoryRepository.findAll(); }

标签: java spring jpa


【解决方案1】:

我在生成 getter 和 setter 时遇到了同样的问题,因为我没有配置我的 lombok

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-21
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    相关资源
    最近更新 更多