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

独自空忆成欢 提交于 2019-12-04 05:01:48

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;

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


}

It seems the way to go is to implement the BeanPersistController, which offers Pre- and Post-Processing options.

To configure it in Play, modify the application.conf file, as such:

ebean.default="models.*,models.adapters.YourPersistController".

I'm really late on this but you can use this : https://gist.github.com/1547244 . You will need to register this Class in you application.conf like this :

ebean.default="models.*, models.sgcore.SGBeanPersistController"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!