Automatic timestamp generation

I need to automatically generate a timestamp when I create a new record and automatically generate a modified timestamp when updating a record.

Someone will tell me how to implement this. I am using openJPA.

early.

+3
source share
2 answers

The easiest way is to use the @Version annotation (documentation here )

Just add the following to your objects:

@Version
private java.sql.Timestamp myTimestamp;

/// normal getters & setters here

And he will do it automatically

+2
source

You can use the following code:

@Column
@Temporal(TemporalType.TIMESTAMP)
private Date creationDate;

@Column
@Temporal(TemporalType.TIMESTAMP)
private Date lastModificationDate;

// getters, setters

@PrePersist
void updateDates() {
  if (creationDate == null) {
    creationDate = new Date();
  }
  lastModificationDate = new Date();
}
+4
source

Source: https://habr.com/ru/post/1713728/


All Articles