【问题标题】:Find by element in Collection按集合中的元素查找
【发布时间】:2021-10-22 20:06:41
【问题描述】:

我正在使用 JPA 编写 Java 中的 Recipe 应用程序。我需要创建一个 findBy 方法来按成分 ID 查找食谱。我试过了:

List<Recipe> findByRecipeIngredientsIngredientIdEqual(Long ingredientId);

但它不起作用

以下是课程:

public class Recipe {

    @OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<RecipeIngredient> recipeIngredients = new ArrayList<>();
}

public class RecipeIngredient {

    @ManyToOne(fetch = FetchType.LAZY)
    @MapsId("ingredientId")
    private Ingredient ingredient;

}

public class Ingredient {

    private Long id;
    private String name;
    
}

【问题讨论】:

    标签: java spring-boot jpa spring-data-jpa spring-data


    【解决方案1】:

    如果我没记错的话,Equal 子句是不必要的,您应该使用以下语句:

    List<Recipe> findByRecipeIngredientsIngredientId(Long ingredientId);
    

    【讨论】:

      猜你喜欢
      • 2012-06-14
      • 1970-01-01
      • 1970-01-01
      • 2012-05-20
      • 1970-01-01
      • 2020-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多