Find by element in Collection

Viewed 111

I'm working on a Recipe application in Java, using JPA. I need to create a findBy method to find a Recipe by Ingredient id. I tried:

List<Recipe> findByRecipeIngredientsIngredientIdEqual(Long ingredientId);

But it doesn't work

Here are the classes:

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;
    
}
1 Answers

If I am not mistaken the Equal clause is not necessary and you should use the following:

List<Recipe> findByRecipeIngredientsIngredientId(Long ingredientId);
Related