Reputation: 5423
I have the following entity:
@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....
}
It is a simple spring boot project, the rule is no column1 data should be when creating a new row(therefore it uses the value 'CREATED') however on the row update I like to change the value to 'ENDED'
Is there a way to tell Hibernate to only do the validation when you create a new row and ignore the validation when you update it?
Upvotes: 0
Views: 564
Reputation: 1014
Take a look at
@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";
}
See https://docs.jboss.org/hibernate/core/4.0/hem/en-US/html/listeners.html
Upvotes: 1