skip to Main Content

I have a table that looks like this:

CREATE TABLE `test_table` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `ref_id` bigint(20) NOT NULL,
  `count` int(11) DEFAULT NULL,
  `status` tinyint(4) NOT NULL,
  `create_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `update_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1

POJO:

@Entity
@Data
@Table(name = "test_table")
public class TestTable implements Serializable {
    private static final long serialVersionUID = 4273269337137129245L;

    @Id
    @Column(name = "id")
    @GenericGenerator(name = "assigned-identity", strategy = "com.paytm.pgplus.datasource.custom.AssignedIdentityGenerator")
    @GeneratedValue(generator = "assigned-identity", strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "ref_id")
    private Long refId;

    @Column(name = "count")
    private Integer count;

    @Column(name = "status")
    @Enumerated(EnumType.ORDINAL)
    private Status status;

    @Column(name = "create_timestamp")
    @Temporal(TemporalType.TIMESTAMP)
    private Date createTimeStamp;

    @Column(name = "update_timestamp")
    @Temporal(TemporalType.TIMESTAMP)
    private Date updateTimeStamp;
}

I am updating this table using Hibernate. I am updating the status column which gets updated successfully. But the expectation is that the update_timestamp column should also get updated accordingly whenever any column gets updated in the test_table which is not happening.

What am I missing here?

2

Answers


  1. Try updating your updateTimeStamp in POJO to:

    @Generated(GenerationTime.ALWAYS)
    @Column(name = "updateTimeStamp")
    @Temporal(TemporalType.TIMESTAMP)
    private Date updateTimeStampt;
    
    Login or Signup to reply.
  2. Actually we need to tell hibernate that we want to update the column on update.

    For that Use @CreationTimestamp (will be set when we insert a record) and @UpdateTimestamp (will be set when the record is updated).

    @CreationTimestamp
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "create_timestamp")
    private Date createTimestamp;
    
    @UpdateTimestamp
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "update_timestamp")
    private Date updateTimestamp;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search