Home > Enterprise >  Update_timestamp not getting updated automatically on table updates
Update_timestamp not getting updated automatically on table updates

Time:10-06

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?

CodePudding user response:

Try updating your updateTimeStamp in POJO to:

@Generated(GenerationTime.ALWAYS)
@Column(name = "updateTimeStamp")
@Temporal(TemporalType.TIMESTAMP)
private Date updateTimeStampt;

CodePudding user response:

Use @CreationTimestamp and @UpdateTimestamp.

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

@UpdateTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "update_timestamp")
private Date updateTimestamp;
  • Related