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条目

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

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

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

最新更新