스프링 데이터 JPA - 컬렉션의 필드의 이름으로 찾기

세르게이 Chernik :

사고와 검색 및 결과 두 가지 일 - 인터넷 문서는 내 질문에 대답하지 않습니다.

내가하는 JPA 저장소 메소드 이름을 구성 할 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)

}

그러나 작품을 does'nt.

레시피:

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;
    }
}
안드로 :

당신은 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)

추천

출처http://43.154.161.224:23101/article/api/json?id=300503&siteId=1