Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring hibernate ListIn查询返回同一项两次_Spring_Spring Boot_Hibernate - Fatal编程技术网

Spring hibernate ListIn查询返回同一项两次

Spring hibernate ListIn查询返回同一项两次,spring,spring-boot,hibernate,Spring,Spring Boot,Hibernate,我正在分类搜索食物。食物模型可以有多个类别。当我试图通过提供分类列表来获取食物时,如果它包括两个类别,它返回的食物是相同食物的2倍 这是我的疑问: findAllByCategoryListInAndDeletedFalseAndFoodNameContaining(search.getCategories(), search.getValue()) 这是我的食物模型: public class Food extends BaseEntity implements Serializable {

我正在分类搜索食物。食物模型可以有多个类别。当我试图通过提供分类列表来获取食物时,如果它包括两个类别,它返回的食物是相同食物的2倍

这是我的疑问:

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());