0

I'm working on a Spring Framework project. For fast development process, I'm using Spring Roo (1.3.2). For several entities, I have requirement to store the creation time and last time it was updated. I've made couple of experiments on Spring Roo tutorial project and this is one of the entities and the way I'm trying to do it.

package com.springsource.roo.pizzashop.domain;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Date;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.springframework.format.annotation.DateTimeFormat;

@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class Topping {

    /**
     */
    @NotNull
    @Size(min = 2)
    private String name;

    /**
     */
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date createdAt;

    /**
     */
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date updatedAt;

    @PrePersist
    protected void onCreate() {
        createdAt= new Date();
    }

    @PreUpdate
    protected void onUpdate() {
      updatedAt = new Date();
    }
}

When I create an object (Topping) it's getting the right value for createdAt and leaving "NULL" for updatedAt. Perfect! That's the way it has to be!

But when I update the object (Topping) it's getting the right value for updatedAt and losing the value of createdAt (NULL).

The problem is that when I update the object I am losing the value of createdAt.

Could you tell me why do I get this behavior? Could you help me with a possible solution for it?

However, createdAt and updatedAt fields don't need to appear in the "view" (create.jspx, update.jspx), so I've set its render attributes to false. These are the forms for creating and updating.

create.jspx

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<div xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:field="urn:jsptagdir:/WEB-INF/tags/form/fields" xmlns:form="urn:jsptagdir:/WEB-INF/tags/form" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:spring="http://www.springframework.org/tags" version="2.0">
    <jsp:directive.page contentType="text/html;charset=UTF-8"/>
    <jsp:output omit-xml-declaration="yes"/>
    <form:create id="fc_com_springsource_roo_pizzashop_domain_Topping" modelAttribute="topping" path="/toppings" render="${empty dependencies}" z="w4+E4tQPCNRPSYVWPNOBUuf9zNE=">
        <field:input field="name" id="c_com_springsource_roo_pizzashop_domain_Topping_name" min="2" required="true" z="GRdEGRLiZ0QLjBH0pTEOZ252BD8="/>
        <field:datetime dateTimePattern="${topping_ts_date_format}" field="ts" id="c_com_springsource_roo_pizzashop_domain_Topping_ts" render="false" z="user-managed"/>
        <field:datetime dateTimePattern="${topping_updated_date_format}" field="updated" id="c_com_springsource_roo_pizzashop_domain_Topping_updated" render="false" z="user-managed"/>
    </form:create>
    <form:dependency dependencies="${dependencies}" id="d_com_springsource_roo_pizzashop_domain_Topping" render="${not empty dependencies}" z="bkqRYdlfs3kDAjK51P0O+7NiahE="/>
</div>

update.jspx

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<div xmlns:field="urn:jsptagdir:/WEB-INF/tags/form/fields" xmlns:form="urn:jsptagdir:/WEB-INF/tags/form" xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
    <jsp:directive.page contentType="text/html;charset=UTF-8"/>
    <jsp:output omit-xml-declaration="yes"/>
    <form:update id="fu_com_springsource_roo_pizzashop_domain_Topping" modelAttribute="topping" path="/toppings" versionField="Version" z="YGl9ujJNQw182uzoCDgi1FdiafQ=">
        <field:input field="name" id="c_com_springsource_roo_pizzashop_domain_Topping_name" min="2" required="true" z="GRdEGRLiZ0QLjBH0pTEOZ252BD8="/>
        <field:datetime dateTimePattern="${topping_ts_date_format}" field="ts" id="c_com_springsource_roo_pizzashop_domain_Topping_ts" render="false" z="user-managed"/>
        <field:datetime dateTimePattern="${topping_updated_date_format}" field="updated" id="c_com_springsource_roo_pizzashop_domain_Topping_updated" render="false" z="user-managed"/>
    </form:update>
</div>
Nkt
  • 11
  • 4

1 Answers1

0

The render="false" on fields tags makes that this fields will not be included on page. So, on POST request, this value is missing and Spring Binding doesn't fill it (take a count that Binding process create new instance of entity, doesn't get it form DB). This makes merge operation to set missing values to null.

For more info about this you can read:

The easiest workaround is include this value in a hidden input of edit form.

On the other hand, take a look to auditing feature of gvNIX. This feature includes auditing (stores creation/update time stamp and user who done it) and and revision log (using Hibernate Envers).

Good luck!

Community
  • 1
  • 1
jmvivo
  • 2,653
  • 1
  • 16
  • 20