这是一个菜鸟问题,我知道,我道歉。我正在尝试使用Hibernates session.merge()方法编辑现有记录,并且我会收到以下错误:
java.lang.IllegalArgumentException: id to load is required for loading
这是我的对象:
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "TITLE_ID", unique = true, nullable = false)
private Integer titleId;
@NotNull
@NotBlank
@Column(name = "TITLE_DESCRIPTION", nullable = false, length = 10)
private String titleDescription;
// default constructor, getters & setters
这是服务层方法:
public void edit(Title title) {
logger.debug("Editing existing title");
// Retrieve session from Hibernate
Session session = sessionFactory.getCurrentSession();
// Retrieve existing title via id
Title existingTitle = (Title) session.get(Title.class, title.getTitleId());
// Assign updated values to this title
existingTitle.setTitleDescription(title.getTitleDescription());
// Save updates
session.merge(existingTitle);
}
这是控制器帖子方法:
@RequestMapping(value="/edit", method = RequestMethod.POST)
public String postEditTitle(@Valid @ModelAttribute("titleAttribute") Title title,
BindingResult result) {
logger.debug("Received request to edit title");
if (result.hasErrors()) {
return "editTitle";
}
else {
titleService.edit(title);
return "redirect:/essays/main/title";
}
}
我想念什么?任何帮助将不胜感激。
问题不是冬眠。当您将其传递给session.get()
时,title.getTitleId()
是无效的,这是您的Web服务/应用程序的问题。
- 您的获取可能无法在模型对象中提供ID
- 您的客户端代码(表单,客户端应用,Ajax调用等等)可能不会保留GET和POST POST 之间的ID
- 您的帖子可能无法在模型对象中提供ID。
您可以在此处提供更多详细信息,或者提出一个新问题,如果您在网络,休息或WS会话中保留属性困难。