在morphia中保存模型时如何忽略`@ PostPersist`?

时间:2012-07-19 15:56:11

标签: morphia

我的模型有一个用@PostPersist注释的方法:

@Entity
class User extends Model {
    private String name;
    // getter and setter
    @PostPersist
    public void _postPersist() {
        // do something
    }
}

现在,当我保存用户时,将自动调用_postPersist()方法。

但是现在我必须保存用户并暂时忽略_postPersist()方法,有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

怎么样?
@Entity
class User extends Model {
    private String name;
    private transient boolean ignorePostPersist = false;
    public User ignorePostPersist() {ignorePostPersist = true; return this;}
    public void ignorePostPersist(boolean b) {ignorePostPersist = b; return this;}
    // getter and setter
    @PostPersist
    public void _postPersist() {
        if (ignorePostPersist) return;
        // do something
    }
    public void saveWithoutPostPersist() {
        ignorePostPersist = true;
        try {
            save();
        } finally {
            ignorePostPersist = false;
        }
    }
}
...
User user = ...
user.saveWithoutPostPersist();