gpt4 book ai didi

java - @ManyToMany 重复条目

转载 作者:行者123 更新时间:2023-12-01 17:41:47 27 4
gpt4 key购买 nike

我在坚持方面遇到了问题。我有一个膳食类(class),其中有产品列表。在 Product 类中是一个 Meals 列表——@ManyToMany 关系。

当我尝试保存它时,编译器想要保存每个产品,但随后产品会在我的数据库中重复。我如何表明产品已经存在?

这是我的代码

@Entity
public class Meal {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@JsonManagedReference
@ManyToMany(cascade = {
CascadeType.MERGE
})
private List<Product> foodList = new ArrayList<>();

@NaturalId
@Column(unique = true, nullable = false)
private String mealName;

private Integer servingWeightGrams = 0;
private Integer servingQty = 0;
private Double nfCalories = 0d;
private Double nfTotalFat = 0d;
...

@Entity
public class Product {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@JsonBackReference
@ManyToMany(mappedBy = "foodList")
private List<Meal> meals = new ArrayList<>();

@Column(unique = false, nullable = false)
private String foodName;
...
@Service
public class MealManager {

MealService mealService;
ProductService productService;

@Autowired
public MealManager(MealService mealService, ProductService productService)
{
this.mealService = mealService;
this.productService = productService;
}

public Meal saveMeal(List<Food> foodList, String mealName){

Meal newMeal = new Meal();
newMeal.setMealName(mealName);

List<Product> productList = parseFoodToProduct(foodList);

productList.stream().forEach(y -> newMeal.getFoodList().add(y));

for(Product food : productList) {

newMeal.setNfCalories(newMeal.getNfCalories() + food.getNfCalories());
newMeal.setNfCholesterol(newMeal.getNfCholesterol() + food.getNfCholesterol());
newMeal.setNfDietaryFiber(newMeal.getNfDietaryFiber() + food.getNfDietaryFiber());
newMeal.setNfP(newMeal.getNfP() + food.getNfP());
newMeal.setNfPotassium(newMeal.getNfPotassium() + food.getNfPotassium());
newMeal.setNfProtein(newMeal.getNfProtein() + food.getNfProtein());
newMeal.setNfSaturatedFat(newMeal.getNfSaturatedFat() + food.getNfSaturatedFat());
newMeal.setNfSodium(newMeal.getNfSodium() + food.getNfSodium());
newMeal.setNfSugars(newMeal.getNfSugars() + food.getNfSugars());
newMeal.setNfTotalCarbohydrate(newMeal.getNfTotalCarbohydrate() + food.getNfTotalCarbohydrate());
newMeal.setNfTotalFat(newMeal.getNfTotalFat() + food.getNfTotalFat());
newMeal.setServingWeightGrams(newMeal.getServingWeightGrams() + food.getServingWeightGrams());

/*if(! productService.ifExists(food.getFoodName())) */ productService.save(food);
}

return mealService.save(newMeal);
}

最佳答案

您可能不需要在 MealManager 中的 for 循环末尾调用 productService.save(food)

您可以尝试在 Meal 类中添加一些实用方法,以保持关系同步,如 this article 中所述。 .

因此,在您的 Meal 类中,您可以添加一个类似的方法

public void addProduct(Product product) {
foodList.add(product);
product.getMeals().add(this);
}

并在MealManager中的for循环内调用此方法,而不是简单地调用newMeal.getFoodList().add(y)

由于您为 ManyToMany 定义了 CascadeType.MERGE,因此关系将在两个实体中同步。

其他观察结果:

  • 我看不到 parseFoodToProduct 方法正在做什么,但假设它从数据库检索必要的产品,您应该将 saveMeal 方法标记为@Transactional

  • 您在 saveMeal 中的 productList 中循环了 2 次...一次使用 stream().forEach()一旦使用增强型 for...我只会在这种情况下保留增强型 for

关于java - @ManyToMany 重复条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60941461/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com