我有以下实体:
@Entity
@Table(name = "bookings")
public class Booking {
@Null
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Null
@Column(name = "column1", columnDefinition = "VARCHAR(25) DEFAULT 'CREATED'")
private String column1;
//the rest of the fields....
}
这是一个简单的 Spring Boot 项目,规则是创建新行时不应该有 column1 数据(因此它使用值“CREATED”),但是在行更新上我喜欢更改“ENDED”的值
有没有办法告诉 Hibernate 仅在创建新行时进行验证,而在更新行时忽略验证?
最佳答案
看看
@PrePersist Executed before the entity manager persist operation is actually executed or cascaded. This call is synchronous with the persist operation.
@PreUpdate Executed before the database UPDATE operation.
@PrePersist
protected void onCreate() {
column1 = "CREATED";
}
@PreUpdate
protected void onUpdate() {
column1 = "ENDED";
}
参见https://docs.jboss.org/hibernate/core/4.0/hem/en-US/html/listeners.html
关于java - JPA Hibernate,如何在创建时进行验证并忽略更新时的验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58416028/