繁体   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