我正在使用 Spring MVC 2.5,并且试图从 GET 请求中加载一个 JSTL 表单对象。我有 Hibernate POJO 作为我的支持对象。
请求中有一个页面指向另一个具有类 ID(行主键)的页面。该请求看起来像“newpage.htm?name=RowId”。这将进入一个带有表单支持对象的页面,
上面的新页面将对象的字段加载到可编辑字段中,并填充了行的现有值。这个想法是,您应该能够编辑这些字段,然后将它们保存回数据库。
此页面的视图看起来像这样
<form:form commandName="thingie">
<span>Name:</span>
<span><form:input path="name" /></span>
<br/>
<span>Scheme:</span>
<span><form:input path="scheme" /></span>
<br/>
<span>Url:</span>
<span><form:input path="url" /></span>
<br/>
<span>Enabled:</span>
<span><form:checkbox path="enabled"/></span>
<br/>
<input type="submit" value="Save Changes" />
</form:form>
控制器里面有这个,
public class thingieDetailController extends SimpleFormController {
public thingieDetailController() {
setCommandClass(Thingie.class);
setCommandName("thingie");
}
@Override
protected Object formBackingObject(HttpServletRequest request) throws Exception {
Thingie thingieForm = (Thingie) super.formBackingObject(request);
//This output is always null, as the ID is not being set properly
logger.debug("thingieForm.getName(): [" + thingieForm.getName() + "]");
//thingieForm.setName(request.getParameter("name"));
SimpleDAO.loadThingie(thingieForm);
return thingieForm;
}
@Override
protected void doSubmitAction(Object command) throws Exception {
Thingie thingie = (Thingie) command;
SimpleDAO.saveThingie(thingie);
}
}
正如您从注释代码中看到的那样,我尝试从请求中手动设置对象 ID(本例为名称)。然而,当我尝试将数据保存在表单中时,Hibernate 抱怨对象被不同步。
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)
这个错误似乎对整个会话有影响,它停止了对我的整个 Web 应用程序的工作,不断地抛出上面看到的过时对象状态异常。
如果熟悉 Spring MVC 的人可以帮助我或提出解决方法,我将不胜感激。
编辑:
会话工厂代码。
private static final SessionFactory sessionFactory;
private static final Configuration configuration = new Configuration().configure();
static {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml)
// config file.
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}