按集合中的元素查找
Find by element in Collection
我正在 Java 中使用 JPA 开发 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;
}
如果我没记错的话,Equal
子句不是必需的,您应该使用以下内容:
List<Recipe> findByRecipeIngredientsIngredientId(Long ingredientId);
我正在 Java 中使用 JPA 开发 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;
}
如果我没记错的话,Equal
子句不是必需的,您应该使用以下内容:
List<Recipe> findByRecipeIngredientsIngredientId(Long ingredientId);