Spring Data JPA - найти по названию поля в коллекции - PullRequest
0 голосов
/ 18 марта 2020

Два дня на обдумывание и поиск и любые результаты - inte rnet и документация не отвечают на мой вопрос.

Мне нужно создать имя метода Jpa Repository для get a Set<Recipe> из базы данных, по , поле Ingredient, ingrName. Я также использую таблицу соединений и сущность RecipeIngredient для хранения количества каждого ингредиента в рецепте, используя RecipeIngredient

Помогите, пожалуйста.

Спасибо!

Я пытаюсь сделать что-то вроде этого:

package com.pck.repository;

import com.pck.Recipe;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Set;

@Repository
public interface RecipeRepository extends JpaRepository<Recipe, Integer> {

    public Set<Recipe> findAllByRecipeIngrs_Ingredient_IngrNameIn(Collection<String> ingredientNames)

}

Но это не работает.

Рецепт:

package com.pck.entity;

import javax.persistence.*;
import java.util.*;

@Entity
@Table(name="recipes")
public class Recipe {

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true, mappedBy = "recipe")
    private Set<RecipeIngredient> recipeIngrs;

    public Recipe() {}

    public Set<RecipeIngredient> getRecipeIngrs() {
        return ingredients;
    }
    public void setRecipeIngrs(Set<RecipeIngredient> recipeIngrs) {
        this.recipeIngrs = recipeIngrs;
    }

    // ... other fields, constructors, getters, setters
}

RecipeIngredient:

package com.pck.entity;

import javax.persistence.*;
import java.util.Objects;

@Entity
@Table(name="recipe_ingredient")
public class RecipeIngredient {

    @JsonIgnore
    @ManyToOne(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH})
    @JoinColumn(name = "recipe_id")
    private Recipe recipe;

    @ManyToOne(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH})
    @JoinColumn(name = "ingredient_id")
    private Ingredient ingredient;

    @Column(name = "ingredient_amount_grams")
    private double ingredientAmountGrams;

    public RecipeIngredient() {}

    public Recipe getRecipe() {
        return recipe;
    }
    public void setRecipe(Recipe recipe) {
        this.recipe = recipe;
    }

    public Ingredient getIngredient() {
        return ingredient;
    }
    public void setIngredient(Ingredient ingredient) {
        this.ingredient = ingredient;
    }

    public double getIngredientAmountGrams() {
        return ingredientAmountGrams;
    }
    public void setIngredientAmountGrams(double ingredientAmountGrams) {
        this.ingredientAmountGrams = ingredientAmountGrams;
    }

    // ... other fields, constructors, getters, setters

}

Ингредиент:

package com.pck.entity;

import javax.persistence.*;
import java.util.*;

@Entity
@Table(name="ingredient")
public class Ingredient{

    @Column(name="ingr_name")
    private String ingrName;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true, mappedBy = "ingredient")
    private Set<RecipeIngredient> recipeIngrs;

    public Ingredient() {}

    public String getIngrName() {
        return ingrName;
    }
    public void setIngrName(String ingrName) {
        this.ingrName = ingrName;
    }

    public Set<RecipeIngredient> getRecipeIngrs() {
        return recipeIngrs;
    }
    public void setRecipeIngrs(Set<RecipeIngredient> recipeIngrs) {
        this.recipeIngrs = recipeIngrs;
    }
}

1 Ответ

2 голосов
/ 18 марта 2020

Вы можете использовать jpql:

@Query("select r from Recipe r inner join r.recipeIngrs ri inner join ri.ingredient i where i.ingrName in :names")
public Set<Recipe> findAllByIngrNameIn(@Param("names") Collection<String> ingredientNames)
...