无法在mysql中保存对象的arraylist-hibernate/spring启动

z9gpfhce  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(380)

我认为hibernate中的关系Map存在一些问题。这是一个配方保存应用程序。
最初,recipe类有一个 String ingredients 储存所有原料的地方。我们意识到这对于在食谱中存储一个成分列表是没有意义的,所以我正在将它重构为我创建的一个新类型的列表, Ingredient : List<Ingredient> ingredients . 配料表单字段是由js在前端动态创建的,当在post请求中提交时,它被转换成新配料的arraylist,然后添加到配方模型中。
每当它到达将其保存到数据库的行时,就会出现以下错误: Field 'ingredients' doesn't have a default value ,它告诉我字段为空。但是,当我使用调试工具时,它显示newrecipe.Ingreents不是null,它实际上是从前端的数据创建的arraylist。
配料类如下所示:

@Entity
public class Ingredient extends AbstractEntity{

    @ManyToOne(targetEntity = Recipe.class,
            fetch = FetchType.LAZY,
            cascade = {CascadeType.MERGE, CascadeType.REMOVE})
    @NotNull(message = "Please include ingredients")
    private Recipe recipe;

    @Id
    @GeneratedValue
    private int id;

    private String ingredient;

    public Ingredient(String ingredient) {
        this.ingredient = ingredient;
    }

    public String getIngredient() {
        return ingredient;
    }

    public void setIngredient(String ingredient) {
        this.ingredient = ingredient;
    }

}

recipe类在这里:

@Entity
public class Recipe extends AbstractEntity {

   private String name;

   @OneToMany(mappedBy = "recipe")
   @NotNull(message = "Ingredients required")
   private List<Ingredient> ingredients = new ArrayList<Ingredient>();

   private String directions;

   @OneToMany(mappedBy = "recipe")
   private List<Instruction> instructions = new ArrayList<Instruction>();

   @NotNull(message = "Category required")
   private Category category;

   private Tag tag;

   private String img;

   @OneToMany(mappedBy = "recipe", cascade = {CascadeType.MERGE, CascadeType.REMOVE})
   @NotNull(message = "User is required")
   private List<UserRecipe> users = new ArrayList<>();

   public Recipe() {
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public Category getCategory() {
      return category;
   }

   public void setCategory(Category category) {
      this.category = category;
   }

   public List<UserRecipe> getUsers() {
      return users;
   }

   public void setUsers(List<UserRecipe> users) {
      this.users = users;
   }

   public String getImg() {
      return img;
   }

   public void setImg(String img) {
      this.img = img;
   }

   public List<Ingredient> getIngredients() {
      return ingredients;
   }

   public void setIngredients(List<Ingredient> ingredients) {
      this.ingredients = ingredients;
   }

   public String getDirections() {
      return directions;
   }

   public void setDirections(String directions) {
      this.directions = directions;
   }

   public Tag getTag() {
      return tag;
   }

   public void setTag(Tag tag) {
      this.tag = tag;
   }

   public List<Instruction> getInstructions() {
      return instructions;
   }

   public void setInstructions(List<Instruction> instructions) {
      this.instructions = instructions;
   }
}

recipecontroller在这里:

@PostMapping("create")
   public String createRecipe(HttpServletRequest request, @ModelAttribute Recipe newRecipe,
                              @ModelAttribute @Valid String newCategory,
                              Errors errors, Model model, RedirectAttributes redirectAttrs) {

      if (errors.hasErrors()) {
         model.addAttribute("title", "Create Recipe");
         return "recipes/create";
      }

      String[] ingredients = request.getParameterValues("ingredient");

      List<Ingredient> ingredientsList = new ArrayList<Ingredient>();

      for (int i = 0; i < ingredients.length; i++) {
         Ingredient newIngredient = new Ingredient(ingredients[i]);
         ingredientsList.add(newIngredient);
      }
      newRecipe.setIngredients(ingredientsList);

      // THIS PRINTS AN ARRAY OF THE NUMBER OF INGREDIENTS ADDED
      System.out.println(ingredientsList.toString());

      // HERE IS WHERE MY ERROR HAPPENS
      Recipe recipe = recipeRepository.save(newRecipe);
      redirectAttrs.addAttribute("recipeId", recipe.getId());

      return "redirect:/recipes/display";
   }

我在这里的想法是,不知何故,我没有Map的成分列表正确的食谱,但我不能找出这一个,经过3天的谷歌搜索和故障排除我在这里。任何帮助都将不胜感激。提前谢谢!

des4xlb0

des4xlb01#

我是个白痴。数据库中的数据类型设置不正确。创建一个新的数据库解决了这个问题。

u1ehiz5o

u1ehiz5o2#

这可能是数据库中架构的问题。您是手动创建模式还是使用自动ddl?如果是手动创建的,则可能在配料表中缺少recipe\u id列。如果这样的联接列具有不同的名称,则必须在Component类上使用@joincolumn覆盖它,如下所示:

@ManyToOne(targetEntity = Recipe.class,
            fetch = FetchType.LAZY,
            cascade = {CascadeType.MERGE, CascadeType.REMOVE})
    @NotNull(message = "Please include ingredients")
    @JoinColumn("the_recipe_id") //****Here you put the join column name you specified****//
    private Recipe recipe;

编辑:还有,你能发布你的abstractentity类吗?这个问题也可能与recipe类中缺少键有关。

相关问题