簡體   English   中英

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

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

我有一個奇怪的問題,當我按照簡單的記錄方式進行操作時,我不明白為什么這樣做有效,我有以下實體:

@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 

}

然后對於新用戶,我調用控制器或服務:

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

我想保存新的用戶和更新用戶,獲取用戶在調試不認為這是有方法的破發點@PrePersist@PreUpdate@PostLoad但它們不是在所有調用,在實際應用中我做的JSON一些轉換字符串以將options映射到properties ,反之亦然。

應當支持: http : //ebean-orm.github.io/docs/features/eventlistening

我正在使用play 2.5.6和sbt-play-ebean 3.0.2。

好吧,我不確定這是一個愚蠢的錯誤還是被誤解了,但是問題是方法的訪問修飾符有誤。

它們必須是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");
}

編輯:為了以防萬一,如果@Transient列修改, @PreUpdatePrePersist將無法正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM