Spring Data JPA - 按集合中字段的名称查找
Spring Data JPA - find by name of field in collection
两天的思考和搜索以及任何结果 - 互联网和文档没有回答我的问题。
我需要构建一个 Jpa Repository 方法名称来 get 从数据库 Set<Recipe>
通过Ingredient
的字段,ingrName
。我还使用连接 table 和实体 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
}
配方成分:
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)
两天的思考和搜索以及任何结果 - 互联网和文档没有回答我的问题。
我需要构建一个 Jpa Repository 方法名称来 get 从数据库 Set<Recipe>
通过Ingredient
的字段,ingrName
。我还使用连接 table 和实体 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
}
配方成分:
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)