简体   繁体   English

在Ebean和Play 2.5.x中使用@PrePersist和@PreUpdate无法正常工作?

[英]Using @PrePersist and @PreUpdate with Ebean and Play 2.5.x not working?

I have strange issue that I can't understand why is this working while I was following simple documented way to do it, I have the following Entity: 我有一个奇怪的问题,当我按照简单的记录方式进行操作时,我不明白为什么这样做有效,我有以下实体:

@Entity
@Table(name = "users")
public class User extends Model {
    @Id
    @Column
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Column
    @CreatedTimestamp
    private DateTime createdDate;

    @Column
    @UpdatedTimestamp
    private DateTime updatedDate;

    @Column
    @Version
    private long version = 0;

    @Column(length = 35, nullable = false)
    @Constraints.Required
    @Constraints.MinLength(2)
    @Constraints.MaxLength(50)
    private String firstName;

    @Column(length = 35, nullable = false)
    @Constraints.Required
    @Constraints.MinLength(2)
    @Constraints.MaxLength(50)
    private String lastName;

    @Column(length = 256)
    @Constraints.MaxLength(256)
    private String jobTitle;

    @Column(length = 1000)
    @JsonIgnore
    private String options;

    @Transient
    private Map<String, Object> properties = new HashMap<>();

    @PrePersist
    protected void prePersist() throws IOException {
        Logger.warn("PrePersist called");
    }

    @PreUpdate
    protected void preUpdate() throws IOException {
        Logger.warn("PreUpdate called");
    }

    @PostLoad
    private void postLoad() throws IOException {
        Logger.warn("PostLoad called");
    }
// settlers and getters here 

}

Then for new user, I call in controller or service: 然后对于新用户,我调用控制器或服务:

User user = new User();
user.setFirstName("Someone").setLastName("Last Name"); // etc
//then 
user.insert();
// or you can even try 
// user.save();

I'm trying to save new user and update user, getting user the break point while debugging not call the methods that have @PrePersist , @PreUpdate and @PostLoad but they are not called at all, in real application I do some conversion from JSON string to Map options to properties and vice versa. 我想保存新的用户和更新用户,获取用户在调试不认为这是有方法的破发点@PrePersist@PreUpdate@PostLoad但它们不是在所有调用,在实际应用中我做的JSON一些转换字符串以将options映射到properties ,反之亦然。

Supposed to be supported : http://ebean-orm.github.io/docs/features/eventlistening 应当支持: http : //ebean-orm.github.io/docs/features/eventlistening

I'm using play 2.5.6 and sbt-play-ebean 3.0.2 . 我正在使用play 2.5.6和sbt-play-ebean 3.0.2。

Well, I'm not sure is this a silly mistake or misunderstood, But the problem was the methods have wrong access modifiers. 好吧,我不确定这是一个愚蠢的错误还是被误解了,但是问题是方法的访问修饰符有误。

They must be a public , not protected or private : 它们必须是public ,而不是protectedprivate

@PrePersist
public void prePersist() throws IOException {
    Logger.warn("PrePersist called");
}

@PreUpdate
public void preUpdate() throws IOException {
    Logger.warn("PreUpdate called");
}

@PostLoad
public void postLoad() throws IOException {
    Logger.warn("PostLoad called");
}

Edit: just in case, if @Transient column modified, @PreUpdate and PrePersist will not work. 编辑:为了以防万一,如果@Transient列修改, @PreUpdatePrePersist将无法正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM