我的模型有一个用@PostPersist
注释的方法:
@Entity
class User extends Model {
private String name;
// getter and setter
@PostPersist
public void _postPersist() {
// do something
}
}
现在,当我保存用户时,将自动调用_postPersist()
方法。
但是现在我必须保存用户并暂时忽略_postPersist()
方法,有没有办法做到这一点?
答案 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();