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

前端 未结 12 2031
忘了有多久
忘了有多久 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:56

    Add the @CreationTimestamp annotation:

    @CreationTimestamp
    @Column(name="timestamp", nullable = false, updatable = false, insertable = false)
    private Timestamp timestamp;
    
    0 讨论(0)
  • 2020-12-12 23:56
    @Column(name = "LastTouched", insertable = false, updatable = false, columnDefinition = "TIMESTAMP default getdate()")
    @Temporal(TemporalType.TIMESTAMP)
    private Date LastTouched;`enter code here`
    
    0 讨论(0)
  • 2020-12-12 23:57

    If you mark your entity with @DynamicInsert e.g.

    @Entity
    @DynamicInsert
    @Table(name = "TABLE_NAME")
    public class ClassName implements Serializable  {
    

    Hibernate will generate SQL without null values. Then the database will insert its own default value. This does have performance implications See [Dynamic Insert][1].

    0 讨论(0)
  • 2020-12-12 23:58

    I fixed the issue by changing the code to

    @Basic(optional = false)
    @Column(name = "LastTouched", insertable = false, updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastTouched;
    

    So the timestamp column is ignored when generating SQL inserts. Not sure if this is the best way to go about this. Feedback is welcome.

    0 讨论(0)
  • 2020-12-12 23:58

    I have this working well using JPA2.0 and MySQL 5.5.10, for cases where I only care about the last time the row was modified. MySQL will create a timestamp on first insertion, and every time UPDATE is called on the row. (NOTE: this will be problematic if I cared whether or not the UPDATE actually made a change).

    The "timestamp" column in this example is like a "last-touched" column.x`

    The code below uses a separate column "version" for optimistic locking.

    private long version;
    private Date timeStamp
    
    @Version
    public long getVersion() {
        return version;
    }
    
    public void setVersion(long version) {
        this.version = version;
    }
    
    // columnDefinition could simply be = "TIMESTAMP", as the other settings are the MySQL default
    @Column(name="timeStamp", columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
    @Temporal(TemporalType.TIMESTAMP)
    public Date getTimeStamp() {
        return timeStamp;
    }
    
    public void setTimeStamp(Date timeStamp) {
        this.timeStamp = timeStamp;
    }
    

    (NOTE: @Version doesn't work on a MySQL "DATETIME" column, where the attribute type is "Date" in the Entity class. This was because Date was generating a value down to the millisecond, however MySQL was not storing the millisecond, so when it did a comparison between what was in the database, and the "attached" entity, it thought they had different version numbers)

    From the MySQL manual regarding TIMESTAMP :

    With neither DEFAULT nor ON UPDATE clauses, it is the same as DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP.
    
    0 讨论(0)
  • 2020-12-12 23:58

    This worked for me:

    @Column(name = "transactionCreatedDate", nullable = false, updatable = false, insertable = false, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
    
    0 讨论(0)
提交回复
热议问题