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

4

7

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

Verticillaster answered 29/3, 2012 at 1:58 Comment(1)
According to github.com/playframework/Play20/pull/113 it should be possible with BeanPersistListener, although I dont know, how to configure it in Play2.Housebroken
X
10

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;
Xenogamy answered 13/2, 2013 at 4:50 Comment(0)
E
4

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


}
Eleonoreleonora answered 9/9, 2012 at 12:38 Comment(0)
A
3

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".

Azotemia answered 3/9, 2012 at 12:26 Comment(0)
R
1

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"
Recur answered 20/12, 2012 at 13:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.