【发布时间】: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(); }