user122591
user122591

Reputation: 151

auto generate timestamp

I have to auto generate timestamp when I am creating a new record and auto generate modified timestamp when I update a record.

can anybody tell me how do I implement this. I am using openJPA.

thanks in advance.

Upvotes: 3

Views: 4814

Answers (2)

David Rabinowitz
David Rabinowitz

Reputation: 30448

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();
}

Upvotes: 4

Gregory Mostizky
Gregory Mostizky

Reputation: 7261

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

Just add the following to your entities:

@Version
private java.sql.Timestamp myTimestamp;

/// normal getters & setters here

And it will do it automatically

Upvotes: 2

Related Questions