Here is my category table.
@Getter
@Setter
@NoArgsConstructor
@Entity
public class Category {
@Id
@GeneratedValue
private long id;
private String name;
private String imageFileName;
private int depth;
@ManyToOne
private Category parentCategory;
@OneToMany(mappedBy = "parentCategory")
private List<Category> childCategories;
@OneToMany(mappedBy = "category")
private List<Product> products;
private boolean deleted;
}
Here is a solution for only 0 and 1 depth categories.
public List<Product> getByCategoryId(long categoryId) {
Category category = categoryService.getById(categoryId);
if (category.getDepth() == 0) {
return productRepository.findByCategoryParentCategoryAndDeletedFalse(category);
}
return productRepository.findByCategoryAndDeletedFalse(category);
}
I want to get all products by category id. For example if i want to get products by category that depth is 10, i have to write so long method. I don't want to write method for every depth. How can i do simplifier?