I have an object which need to be updated, the following form is used to update it.
I have an attribute called createDate
that I do not want to be changed when I update the record therefore I did not include it in the form, but when I submit the form it changes the value of createDate
to null.
<s:form method="POST" autocomplete="on" action="update">
<input type="hidden" name="user.id" value="${user.id}"/>
....
</s:form>
Code
final Session session = HibernateUtil.getSession();
try {
final Transaction tx = session.beginTransaction();
try {
session.update(user);
if (!tx.wasCommitted()) {
tx.commit();
}
return "SUCCESS";
} catch (Exception e) {
tx.rollback();
e.printStackTrace();
return "Failed";
}
} finally {
HibernateUtil.closeSession();
}
User class
....
@Column(name = "createdate")
@Temporal(javax.persistence.TemporalType.TIMESTAMP)
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
I supposed it changes the value to null
because the field has not been included in the form so I added the following code to my form, although the HTML source of the page show the correct value of createDate
but when I submit the form it throws following exception.
<input type="hidden" name="user.createDate" value="${user.createDate}"/>
Exception after adding input hidden for createDate
INFO: 2014-02-26 12:37:25,896 - WARN [CommonsLogger.java:60] ognl.MethodFailedException: Method "setCreateDate" failed for object com.myproject.myclasses.User@2d680c36 [java.lang.NoSuchMethodException: com.myproject.myclasses.User.setCreateDate([Ljava.lang.String;)]
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1305)
at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:1494)
at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:85)
at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:162)
at com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.setProperty(ObjectAccessor.java:27)
......