mysql - 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 数据库时一切正常,但是,当将制造商添加到数据库时,会创建一个完全为空的额外记录:
我已经尝试了几个小时来解决这个问题,但没有运气。任何建议或指出我正确的方向将不胜感激。
解决方案
在Manufacturer
类中,您可以尝试从注释中删除cascade
参数。@OneToMany
这是一篇您可能会觉得有用的好文章:https ://www.baeldung.com/hibernate-one-to-many
推荐阅读
- php - 发布表单时,Laravel Route 资源不会更新
- java - 在同一浏览器 Selenium (JAVA) 中同时打开多个 URL
- javascript - 如何使用javascript查找块并替换长字符串
- asp.net-mvc - 如何在不使用配置转换的情况下将 Azure webapp 部署到多个环境
- ios - iOS 11:是否可以不显示通知警报(作为横幅),但应该在通知中心添加?
- c# - 不可调用成员 'xyz' 不能像方法一样使用,vb 和 c# 之间的区别
- mysql - 如何解决错误:ERROR 1442 (HY000) Can't update table 'table name' in stored function/trigger?
- redhat - 创建邮箱文件:文件存在
- android - 添加到库后如何在 Android Studio 中使用 JSONLibrary
- python - 仓库库存需求管理