0

我有一个模型,它有一个方法注释@PostPersist

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

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

但是现在我必须保存一个用户并_postPersist()暂时忽略该方法,有什么办法吗?

4

1 回答 1

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();
于 2012-07-19T22:11:45.480 回答