Spring MVC + Hibernate:加载时需要加载id [英] Spring MVC + Hibernate: id to load is required for loading
问题描述
这是一个小问题,我知道,我很抱歉。我试图用Hibernates session.merge()方法编辑现有记录,我得到以下错误:
java.lang .IllegalArgumentException:要加载的id是加载
所必需的,这是我的对象:
@Id
@GeneratedValue(策略= IDENTITY)
@Column(名称= title_id的,唯一=真,可为空=假)
private Integer titleId;
@NotNull
@NotBlank
@Column(name =TITLE_DESCRIPTION,nullable = false,length = 10)
private String titleDescription;
//默认构造函数,getters& setters
这是服务层方法:
public void edit(Title title){
logger.debug(Editing existing title);
//从Hibernate检索会话
Session session = sessionFactory.getCurrentSession();
//通过id
检索现有标题Title existingTitle =(Title)session.get(Title.class,title.getTitleId());
//为此标题分配更新值
existingTitle.setTitleDescription(title.getTitleDescription());
//保存更新
session.merge(existingTitle);
}
这是控制器的POST方法:
@RequestMapping(值= /编辑,方法= RequestMethod.POST)
公共字符串postEditTitle(@Valid @ModelAttribute( titleAttribute)标题标题,
BindingResult结果){
logger.debug(接收到编辑标题的请求);
if(result.hasErrors()){
returneditTitle;
}
else {
titleService.edit(title);
返回redirect:/ essays / main / title;
}
}
我错过了什么?任何帮助将不胜感激。
问题不在于Hibernate。 title.getTitleId()在您传递给 session.get()
时为null,这是您的问题web服务/应用程序。
- 您的GET可能不会在模型对象中提供id
- 您的客户端代码(表单,客户端应用程序,ajax调用,不管它是什么)可能不会保留GET和POST之间的ID
- 您的POST可能未提供模型对象中的id 。
您可以在此处提供更多详细信息,或者提出一个新问题,如果您在跨网络保留属性时遇到困难,或WS会话。
This is such a noob question, I know and I apologize. I'm trying to edit existing record with Hibernates session.merge() method and I get the following error:
java.lang.IllegalArgumentException: id to load is required for loading
This is my object:
@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
This is service layer method:
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);
}
This is controller POST method:
@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";
}
}
What am I missing? Any help will be much appreciated.
The problem isn't with Hibernate. title.getTitleId()
is null when you pass it to session.get()
, and that's a problem with your web service/application.
- Your GET might not be providing the id in the model object
- Your client code (form, client app, ajax call, whatever it is) might not be retaining the ID between the GET and POST
- Your POST might not be providing the id in the model object.
You can provide more details here, or ask a new question, if you're having difficulties retaining attributes across the web, rest or WS session.
这篇关于Spring MVC + Hibernate:加载时需要加载id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!