Setting a JPA timestamp column to be generated by the database?

前端 未结 12 2033
忘了有多久
忘了有多久 2020-12-12 23:19

In my SQL Server 2000 database, I have a timestamp (in function not in data type) column of type DATETIME named lastTouched set to getdate()<

12条回答
  •  南方客
    南方客 (楼主)
    2020-12-12 23:59

    If you are doing development in Java 8 and Hibernate 5 Or Spring Boot JPA then use following annotation directly in your Entity class. Hibernate gets the current timestamp from the VM and will insert date and time in database.

    public class YourEntity {
     
        @Id
        @GeneratedValue
        private Long id;
     
        private String name;
     
        @CreationTimestamp
        private LocalDateTime createdDateTime;
     
        @UpdateTimestamp
        private LocalDateTime updatedDateTime;
     
        …
    }
    

提交回复
热议问题