Spring hibernate ListIn查询返回同一项两次
我正在分类搜索食物。食物模型可以有多个类别。当我试图通过提供分类列表来获取食物时,如果它包括两个类别,它返回的食物是相同食物的2倍 这是我的疑问:Spring hibernate ListIn查询返回同一项两次,spring,spring-boot,hibernate,Spring,Spring Boot,Hibernate,我正在分类搜索食物。食物模型可以有多个类别。当我试图通过提供分类列表来获取食物时,如果它包括两个类别,它返回的食物是相同食物的2倍 这是我的疑问: findAllByCategoryListInAndDeletedFalseAndFoodNameContaining(search.getCategories(), search.getValue()) 这是我的食物模型: public class Food extends BaseEntity implements Serializable {
findAllByCategoryListInAndDeletedFalseAndFoodNameContaining(search.getCategories(), search.getValue())
这是我的食物模型:
public class Food extends BaseEntity implements Serializable {
@ManyToMany(cascade = {CascadeType.ALL})
private List<Category> categoryList; }
}好的,我解决了这个问题,但我认为不应该用这种方法解决
List<Food> foodList = foodRepo.
findAllByCategoryListInAndDeletedFalseAndFoodNameContaining(search.getCategories(), search.getValue());
foodList.stream().distinct().collect(Collectors.toList());
List foodList=foodRepo。
findAllByCategoryListInAndDeletedFalseAndFoodNameContaining(search.getCategories(),search.getValue());
foodList.stream().distinct().collect(Collectors.toList());
如果您确定数据库中没有重复项,请使用spring引导配置spring.jpa.show sql=true
打印执行的sql,以便更好地了解您的查询实际执行的操作。谢谢,我会尝试
List<Food> foodList = foodRepo.
findAllByCategoryListInAndDeletedFalseAndFoodNameContaining(search.getCategories(), search.getValue());
foodList.stream().distinct().collect(Collectors.toList());