Spring MVC 2.5を使用しており、GETリクエストからロードするJSTLフォームオブジェクトを取得しようとしています。バッキングオブジェクトとしてHibernatePOJOがあります。
リクエストにクラスID(行の主キー)を持つ別のページに向かう1つのページがあります。リクエストは「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アプリケーション全体で機能しなくなり、上記のStale ObjectStateExceptionが継続的にスローされます。
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;
}