【发布时间】:2016-09-01 16:17:48
【问题描述】:
我有一个奇怪的问题,我不明白为什么这在我遵循简单的记录方法时能正常工作,我有以下实体:
@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。
【问题讨论】:
-
你的
User类不扩展com.avaje.ebean.Model吗?如果你改用Ebean.save(user)会发生什么? -
@是的,我扩展了模型,很抱歉错误地删除了它,是的,尝试了
Ebean.save(user),但没有运气,我在事务块内部尝试过,也没有运气。 -
公开方法
-
@MohdAlomar 非常感谢,我的愚蠢错误 :)
标签: java playframework orm ebean