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");
}