卡路里计数器应用程序 - 尝试将 FoodProduct 添加到 Meal 时出现异常

时间:2021-01-08 08:02:55

标签: java spring hibernate

我正在开发卡路里计数器应用程序。我有下面介绍的课程,但是当我尝试将 FoodProduct 添加到 Meal 时出现异常。

膳食课

package pl.sosinski.nutritioncounter.model;

import javax.persistence.*;
import java.time.LocalDate;
import java.util.List;

@Entity
@Table(name = "meals")
public class Meal {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

    @Column(name = "created_at")
    private LocalDate date = LocalDate.now();

    @ManyToOne
    @JoinColumn(name = "user_id")
    private User user;

    @OneToMany(mappedBy = "meal")
    private List<MealFoodProduct> mealFoodProducts;

    public Meal() {
    }


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public List<MealFoodProduct> getMealFoodProducts() {
        return mealFoodProducts;
    }

    public void setMealFoodProducts(List<MealFoodProduct> mealFoodProducts) {
        this.mealFoodProducts = mealFoodProducts;
    }

    public LocalDate getDate() {
        return date;
    }

    public void setDate(LocalDate date) {
        this.date = date;
    }


    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}

食品类

package pl.sosinski.nutritioncounter.model;

import javax.persistence.*;

@Entity
@Table(name = "food_products")
public class FoodProduct {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

    @Column(name = "product_name")
    private String productName;

    @Column(name = "proteins")
    private Double proteins;

    @Column(name = "fats")
    private Double fats;

    @Column(name = "carbohydrates")
    private Double carbohydrates;

    @Column(name = "calories")
    private Double calories;


    public FoodProduct() {
    }


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public Double getProteins() {
        return proteins;
    }

    public void setProteins(Double proteins) {
        this.proteins = proteins;
    }

    public Double getFats() {
        return fats;
    }

    public void setFats(Double fats) {
        this.fats = fats;
    }

    public Double getCarbohydrates() {
        return carbohydrates;
    }

    public void setCarbohydrates(Double carbohydrates) {
        this.carbohydrates = carbohydrates;
    }

    @Override
    public String toString() {
        return productName;
    }

    public Double getCalories() {
        return calories;
    }

    public void setCalories(Double calories) {
        this.calories = calories;
    }
}

MealFoodProduct 类

package pl.sosinski.nutritioncounter.model;

import javax.persistence.*;

@Entity
@Table(name = "meals_food_products")
@IdClass(MealFoodProductPK.class)
public class MealFoodProduct {

    @Id
    @ManyToOne
    @JoinColumn(name = "meal_id")
    private Meal meal;

    @Id
    @ManyToOne
    @JoinColumn(name = "food_product_id")
    private FoodProduct foodProduct;

    @Column(name = "food_product_weight")
    private double weight;

    public MealFoodProduct() {
    }


    public Meal getMeal() {
        return meal;
    }

    public void setMeal(Meal meal) {
        this.meal = meal;
    }

    public FoodProduct getFoodProduct() {
        return foodProduct;
    }

    public void setFoodProduct(FoodProduct foodProduct) {
        this.foodProduct = foodProduct;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }
}

MealFoodProductPK 类 - MealFoodProduct 类的复合键

package pl.sosinski.nutritioncounter.model;

import java.io.Serializable;
import java.util.Objects;

public class MealFoodProductPK implements Serializable {

    private Meal meal;
    private FoodProduct foodProduct;

    public MealFoodProductPK() {
    }

    public MealFoodProductPK(Meal meal, FoodProduct foodProduct) {
        this.meal = meal;
        this.foodProduct = foodProduct;
    }

    public Meal getMeal() {
        return meal;
    }

    public void setMeal(Meal meal) {
        this.meal = meal;
    }

    public FoodProduct getFoodProduct() {
        return foodProduct;
    }

    public void setFoodProduct(FoodProduct foodProduct) {
        this.foodProduct = foodProduct;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MealFoodProductPK that = (MealFoodProductPK) o;
        return meal.equals(that.meal) &&
                foodProduct.equals(that.foodProduct);
    }

    @Override
    public int hashCode() {
        return Objects.hash(meal, foodProduct);
    }
}

这是将新的 FoodProduct 添加到 Meal 的代码

   //...

        MealFoodProduct mealFoodProduct = new MealFoodProduct();
        FoodProduct foodProduct = foodProductService.findById(1);
        Meal meal = mealService.findById(1);

        mealFoodProduct.setFoodProduct(foodProduct);
        mealFoodProduct.setMeal(meal);
        mealFoodProduct.setWeight(100);

        mealFoodProductService.save(mealFoodProduct);

   //...

当我尝试使用此代码向 Meal 添加新的 FoodProduct 时,出现此异常:

Failed to convert property value of type 'java.lang.Integer' to required type 'pl.sosinski.nutritioncounter.model.FoodProduct' for property 'foodProduct'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.Integer' to required type 'pl.sosinski.nutritioncounter.model.FoodProduct' for property 'foodProduct': no matching editors or conversion strategy found
org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.Integer' to required type 'pl.sosinski.nutritioncounter.model.FoodProduct' for property 'foodProduct'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.Integer' to required type 'pl.sosinski.nutritioncounter.model.FoodProduct' for property 'foodProduct': no matching editors or conversion strategy found

MealFoodProductService

@Service
public class MealFoodProductService {
    private final MealFoodProductRepository mealFoodProductRepository;

    @Autowired
    public MealFoodProductService(MealFoodProductRepository mealFoodProductRepository) {
        this.mealFoodProductRepository = mealFoodProductRepository;
    }

    public void deleteFoodProductFromMeal(int mealId, int foodProductId) {
        mealFoodProductRepository.deleteFoodProductFromMeal(mealId, foodProductId);
    }

    public void save(MealFoodProduct mealFoodProduct) {
        mealFoodProductRepository.save(mealFoodProduct);
    }
}

MealFoodProductRepository

@Repository
public interface MealFoodProductRepository extends JpaRepository<MealFoodProduct, MealFoodProductPK> {

    @Modifying
    @Transactional
    @Query(value = "delete from MealFoodProduct mpf where mpf.meal.id= ?1 and mpf.foodProduct.id = ?2")
    public void deleteFoodProductFromMeal(int mealId, int foodProductId);
}

MainController.addFoodProductForm

@GetMapping("/addFoodProductForm")
public String addFoodProductForm(){

    MealFoodProduct mealFoodProduct = new MealFoodProduct();
    FoodProduct foodProduct = foodProductService.findById(1);
    Meal meal = mealService.findById(1);

    mealFoodProduct.setFoodProduct(foodProduct);
    mealFoodProduct.setMeal(meal);
    mealFoodProduct.setWeight(100);

    mealFoodProductService.save(mealFoodProduct);

    return "redirect:/";
} 

0 个答案:

没有答案