Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use @PrePersist and @PreUpdate with eBean and Play! 2.0?

I want to know if is it possible to use @PrePersist and @PreUpdate with Ebean and Play! 2.0. If so how is this feature activated. I've seen that there was a pull request adding this feature a month ago, but I can't make this work on Play 2.0.

Thanks

like image 276
GuidoMB Avatar asked Mar 29 '12 01:03

GuidoMB


2 Answers

If your goal is just setting createdAt or updatedAt fields, and you're using EBean, try @CreatedTimestamp and @UpdatedTimestamp. See here. I'd prefer to use Biesior's approach, but it seemed to fail on Cascades -- the methods were never called.

@Column(name="created_at")
@CreatedTimestamp
private Date createdAt;

@Column(name="updated_at")
@UpdatedTimestamp
private Date updatedAt;
like image 174
liberty Avatar answered Oct 09 '22 18:10

liberty


Not a direct answer, but you can simulate these features by overriding methods of Model class in your model, sample:

public class Post extends Model {

    // .... 

    @Override
    public void save() {
        this.createDate = new Date();
        this.modifyDate = new Date();
        super.save();
    }

    @Override
    public void update(Object o) {
        this.modifyDate = new Date();
        super.update(o);
    }


}
like image 43
biesior Avatar answered Oct 09 '22 19:10

biesior