Java Spring数据JPA-按集合中字段的名称查找

Java Spring数据JPA-按集合中字段的名称查找,java,spring,hibernate,spring-data,jpql,Java,Spring,Hibernate,Spring Data,Jpql,两天的思考和搜索以及任何结果——互联网和文档都不能回答我的问题 我需要构建一个Jpa存储库方法名,以便从数据库中获取集,通过成分的字段,ingrName。我还使用联接表和实体RecipeIngredient,使用RecipeIngredient 请帮忙 谢谢 我试着做这样的事情: package com.pck.repository; import com.pck.Recipe; import org.springframework.data.jpa.repository.JpaReposit

两天的思考和搜索以及任何结果——互联网和文档都不能回答我的问题

我需要构建一个Jpa存储库方法名,以便从数据库中获取
,通过
成分的字段
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.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;
    }
}
package com.pck.entity;
导入javax.persistence.*;
导入java.util.*;
@实体
@表(名称=“成分”)
公共类成分{
@列(name=“ingr\u name”)
私有字符串ingrName;
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY,orphanRemoving=true,mappedBy=“component”)
专用集接收器;
公共成分(){}
公共字符串getIngrName(){
返回ingrName;
}
公共无效设置名称(字符串名称){
this.ingrName=ingrName;
}
公共集getRecipeIngrs(){
返回接收器;
}
公共无效设置收件人(设置收件人){
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)
@Query(“从配方r内部连接r.recipeIngrs ri内部连接ri.i中选择r,其中i.ingrName in:名称”)
公共集FindAllBingRNAMEIN(@Param(“名称”)集合ingredientNames)
您可以利用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)
@Query(“从配方r内部连接r.recipeIngrs ri内部连接ri.i中选择r,其中i.ingrName in:名称”)
公共集FindAllBingRNAMEIN(@Param(“名称”)集合ingredientNames)

谢谢,它很有效。不幸的是,没有方法只使用Spring数据中的查询方法来实现这一点。@SergeyChernik我不知道任何方法。没问题,祝你今天愉快!非常感谢,祝你今天愉快!谢谢,它很有效。不幸的是,没有方法只使用Spring数据中的查询方法来实现这一点。@SergeyChernik我不知道任何方法。没问题,祝你今天愉快!非常感谢,祝你今天愉快!