首页 > 解决方案 > Hibernate & MySQL:创建记录时,添加第二条记录并且完全为空

问题描述

我对 spring 和 hibernate 还很陌生,我正在创建一个简单的应用程序,它有两个由 @OneToMany 和 @ManyToOne 关系链接的实体类。

@Entity
public class ActiveIngredient {

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

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

    @Column(name="active")
    private String active;

    @Column(name="potency")
    private double potency;

    @ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE,
            CascadeType.DETACH, CascadeType.REFRESH})
    @JoinColumn(name="manufacturer")
    private Manufacturer manufacturer;

@Entity
public class Manufacturer {

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

    @Column(name="name")
    private String manName;

    @Column(name="country")
    private String country;

    @Column(name="city")
    private String city;

    @Column(name="email")
    private String email;

    @Column(name="phone")
    private String phone;

    @OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE,
            CascadeType.DETACH, CascadeType.REFRESH}, mappedBy = "manufacturer")
    private List<ActiveIngredient> activeIngredients;

    @OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE,
            CascadeType.DETACH, CascadeType.REFRESH}, mappedBy = "manufacturer")
    private List<ExcipientIngredient>excipientIngredients;

在前端,使用百里香模板,用户可以在其中提交数据以创建成分和制造商。下面是控制器:

@Controller
@RequestMapping("/excipients")
public class ExcipientIngredientController {

    @Autowired
    private ExcipientIngredientService excipientIngredientService;
    @Autowired
    private ManufacturerService manufacturerService;

    @GetMapping("/listExc")
    public String listExcipients(Model model) {
        List<ExcipientIngredient> theExcipient = excipientIngredientService.findAll();
        //add list to the model.
        model.addAttribute("excipient", theExcipient);
        //return the thymeleaf page with this path.
        return "list-excipients";
    }

    @GetMapping("/addExcipientForm")
    public String addForm(Model model){
        //to add a new excipient ingredient, need to create a new object
        ExcipientIngredient excipientIngredient = new ExcipientIngredient();
        Manufacturer manufacturer = new Manufacturer();

        //add to the model
        model.addAttribute("excipientIngredient", excipientIngredient);
        model.addAttribute("manufacturer", manufacturer);

        return "add-excipient-form";
    }

    @PostMapping("/saveExc")
    public String saveExcipients(@ModelAttribute("excipientIngredient") ExcipientIngredient excipientIngredient, @ModelAttribute("manufacturer") Manufacturer manufacturer) {

        // save the Ingredient
        excipientIngredientService.saveOrUpdate(excipientIngredient);
        manufacturerService.saveOrUpdate(manufacturer);

        // use a redirect to prevent duplicate submissions
        return "redirect:/excipients/listExc";
    }
}

这是保存/更新方法的实现。

@Override
    public void saveOrUpdate(ExcipientIngredient excipientIngredient) {

        Session currentSession = entityManager.unwrap(Session.class);

        currentSession.saveOrUpdate(excipientIngredient);
    }

创建成分并更新到 MySQL 数据库时一切正常,但是,当将制造商添加到数据库时,会创建一个完全为空的额外记录:

MySQL 入口

我已经尝试了几个小时来解决这个问题,但没有运气。任何建议或指出我正确的方向将不胜感激。

标签: mysqlspring-boothibernate

解决方案


Manufacturer类中,您可以尝试从注释中删除cascade参数。@OneToMany

这是一篇您可能会觉得有用的好文章:https ://www.baeldung.com/hibernate-one-to-many


推荐阅读