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

前端 未结 12 2032
忘了有多久
忘了有多久 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;
     
        …
    }
    
    0 讨论(0)
  • 2020-12-13 00:06
    @Column(nullable = false, updatable = false)
    @CreationTimestamp
    private Date created_at;
    

    this worked for me. more info

    0 讨论(0)
  • 2020-12-13 00:08

    This also works for me:-

    @Temporal(TemporalType.TIMESTAMP)   
    @Column(name = "CREATE_DATE_TIME", nullable = false, updatable = false, insertable = false, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
    public Date getCreateDateTime() {
        return createDateTime;
    }
    
    public void setCreateDateTime(Date createDateTime) {
        this.createDateTime = createDateTime;
    }
    
    0 讨论(0)
  • 2020-12-13 00:10

    I'm posting this for people searching for an answer when using MySQL and Java Spring Boot JPA, like @immanuelRocha says, only have too @CreationTimeStamp to the @Column in Spring, and in MySQL set the default value to "CURRENT_TIMESTAMP".

    In Spring add just the line :

    @Column(name = "insert_date")
    @CreationTimestamp
    private Timestamp insert_date;

    0 讨论(0)
  • 2020-12-13 00:15

    I do not think that every database has auto-update timestamps (e.g. Postgres). So I've decided to update this field manually everywhere in my code. This will work with every database:

    thingy.setLastTouched(new Date());
    HibernateUtil.save(thingy);
    

    There are reasons to use triggers, but for most projects, this is not one of them. Triggers dig you even deeper into a specific database implementation.

    MySQL 5.6.28 (Ubuntu 15.10, OpenJDK 64-Bit 1.8.0_66) seems to be very forgiving, not requiring anything beyond

    @Column(name="LastTouched")
    

    MySQL 5.7.9 (CentOS 6, OpenJDK 64-Bit 1.8.0_72) only works with

    @Column(name="LastTouched", insertable=false, updatable=false)
    

    not:

    FAILED: removing @Temporal
    FAILED: @Column(name="LastTouched", nullable=true)
    FAILED: @Column(name="LastTouched", columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
    

    My other system info (identical in both environments)

    • hibernate-entitymanager 5.0.2
    • hibernate-validator 5.2.2
    • mysql-connector-java 5.1.38
    0 讨论(0)
  • 2020-12-13 00:21

    I realize this is a bit late, but I've had success with annotating a timestamp column with

    @Column(name="timestamp", columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
    

    This should also work with CURRENT_DATE and CURRENT_TIME. I'm using JPA/Hibernate with Oracle, so YMMV.

    0 讨论(0)
提交回复
热议问题