ripper234
ripper234

Reputation: 230028

How to create an auto-generated Date/timestamp field in a Play! / JPA?

I'd like to add a Date/DateTime/Timestamp field on an entity object, that will be automatically created when the entity is created/persisted and set to "now", never to be updated again.

Other use cases included fields that were always updated to contain the last modification date of the entity.

I used to achieve such requirements in the mysql schema.

What's the best way to do this in Play! / JPA?

Upvotes: 20

Views: 59879

Answers (3)

Piotr Nowicki
Piotr Nowicki

Reputation: 18174

I think you can achieve it in at least two ways.

Database default value

I think the easiest way would be to mark the column as updatable=false, insertable=false by using @Column (this will give you immutability - the JPA will not include this column into INSERT and UPDATE statements) and setting the database column to have a default value of NOW.

JPA Lifecycle callback methods

Other way would be to provide a @PrePersist lifecycle callback method which would set your date object to the actual date new Date(). Then you would need to make sure no one will edit this value, so you shouldn't provide any setters for this property.

If you want the date to be updated when entity is modified you could, similarly, implement @PreUpdate lifecycle callback method which would set the actual modification date.

Just remember that if you're working with Date objects you should do a defensive copy of your Date object (so you should return something like new Date(oldDate.getTime()); instead of plain return oldDate).
This will prevent users from using getter of your Date and modifying its state.

Upvotes: 25

marcospereira
marcospereira

Reputation: 12214

There is a code snippet that you can adapt to achieve what you want. Take a look:

// Timestampable.java

package models;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Version;

import play.db.ebean.Model;

@MappedSuperclass
public class Timestampable extends Model {

  @Id
  @GeneratedValue
  public Long id;

  @Column(name = "created_at")
  public Date createdAt;

  @Column(name = "updated_at")
  public Date updatedAt;

  @Version
  public int version;

  @Override
  public void save() {
    createdAt();
    super.save();
  }

  @Override
  public void update() {
    updatedAt();
    super.update();
  }

  @PrePersist
  void createdAt() {
    this.createdAt = this.updatedAt = new Date();
  }

  @PreUpdate
  void updatedAt() {
    this.updatedAt = new Date();
  }
}

Upvotes: 41

lirugo
lirugo

Reputation: 71

I think it's a little bit clearly

    @Column(name = "created_at")
    @CreationTimestamp
    private LocalDateTime createdAt;

    @Column(name = "updated_at")
    @UpdateTimestamp
    private LocalDateTime updatedAt;

Upvotes: 7

Related Questions