Ion Crismaru
Ion Crismaru

Reputation: 31

Spring JPA property to select objects that contains fields from List

I have entity Recipe:

public class Recipe extends BaseEntity{
// other fields

@Column(name = "ingredients")
@ManyToMany
@JoinTable(name = "ingredients_recipes",
        joinColumns = {@JoinColumn(name = "recipe_id", referencedColumnName = "id")},
        inverseJoinColumns = {@JoinColumn(name = "ingredient_id", referencedColumnName = "id")})
private List<Ingredient> ingredients;

Ingredient:

public class Ingredient extends BaseEntity {
private String name;

@Column(name = "recipes")
@ManyToMany(mappedBy = "ingredients", fetch = FetchType.LAZY)
private List<Recipe> recipes;

I need to select all recipes that contain all or less ingredients from a List. I tried

Set<Recipe> findAllByIngredientsIn(List<Ingredient> ingredientList);

but it checks that the recipe has each ingredient individually. If I give only salt in ingredientList, it return recipes with salt and peper(or other ingredients), but I need to get recipes that only have salt. Is there a jpa repository method that I can use, or I need only a custom query?

Upvotes: 0

Views: 1614

Answers (1)

Jens Schauder
Jens Schauder

Reputation: 81907

Is there a jpa repository method that I can use, or I need only a custom query?

No you'll have to come up with your own query and use it in a @Query annotation.

Upvotes: 2

Related Questions