JavaDeveloper
JavaDeveloper

Reputation: 102

Spring Data Jpa : Regarding creation and update time stamp (MySQL)

I am writing rest APIs using Spring boot and here is the structure of my entity

@Entity
@Table(name="employee")
@Data
Public class employee extends Serializable {
    Employee attributes such as name,dept,etc

    @CreationTimestamp
    @Temporal(Temporal type.TIMESTAMP)
    @Column(name="create_time") 
    private Date create_time;

    @UpdateTimestamp
    @Temporal(Temporal type.TIMESTAMP)
    @Column(name="dtm_of_last_edit") 
    private Date dtmOfLastEdit;

}

Now the problem is every time I insert the record, JPA Auto inserts create and update time but on UPDATE (PUT CALL) I get error saying create time cannot be null...I tried sending the create time in payload but they did not help. Am I missing something in entity ?

Upvotes: 0

Views: 2930

Answers (2)

Syed Mehtab Hassan
Syed Mehtab Hassan

Reputation: 1337

Try Using so that this field will not be updated

@Column(name="create_time", updatable = false)

Upvotes: 1

Boris
Boris

Reputation: 45

You better use prePersist and preUpdate method. Here is the link https://vladmihalcea.com/prepersist-preupdate-embeddable-jpa-hibernate/ so you can have better idea.

Upvotes: 0

Related Questions