Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Ebian和Play 2.5.x上使用@PrePersist和@PreUpdate不起作用?_Java_Playframework_Orm_Ebean - Fatal编程技术网

Java 在Ebian和Play 2.5.x上使用@PrePersist和@PreUpdate不起作用?

Java 在Ebian和Play 2.5.x上使用@PrePersist和@PreUpdate不起作用?,java,playframework,orm,ebean,Java,Playframework,Orm,Ebean,我有一个奇怪的问题,我不明白为什么这是工作,而我是按照简单的记录方式来做,我有以下实体: @Entity @Table(name = "users") public class User extends Model { @Id @Column @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @Column @CreatedTimestamp priva

我有一个奇怪的问题,我不明白为什么这是工作,而我是按照简单的记录方式来做,我有以下实体:

@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 

}
我正在尝试保存新用户和更新用户,在调试时获取用户断点,而不调用具有
@PrePersist
@PreUpdate
@PostLoad
的方法,但它们根本没有被调用,在实际应用程序中,我将JSON字符串转换为映射
选项
属性
,反之亦然

应该得到支持:


我正在使用play 2.5.6和3.0.2。

我不确定这是一个愚蠢的错误还是被误解了,但问题是这些方法的访问修饰符错误

它们必须是
公共的
,而不是
受保护的
私有的

@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
列,
@PreUpdate
PrePersist
将不起作用。

您的
用户
类是否扩展了
com.avaje.ebean.Model
?如果您改用
Ebean.save(user)
,会发生什么情况?@是的,我扩展了模型,很抱歉错误地删除了它,是的,我尝试了
Ebean.save(user)
,但运气不好,我在事务块内尝试了,也没有运气。使用该方法public@MohdAlomar非常感谢,我愚蠢的错误:)请注意,\@Post。。。和\@Pre。。。也不适用于\@Lob列
@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");
}