Reputation: 62356
@javax.persistence.Column(name = "created", nullable = false, insertable = true, updatable = true, length = 19, precision = 0)
@Basic
public Timestamp getCreated() {
return created;
}
public void setCreated(Timestamp created) {
this.created = created;
}
I have this entity and I would like for MySQL to provide the created timestamp. I've tried adding @GeneratedValue
and nullable = true
and it still fails when saving saying the value cannot be null.
I understand my entity is passing null because that's the default value so MySQL is throwing an error because I'm trying to pass null and it's not set as a NULl field.
However, is it possible for me to work around this on a java object level? Say, not submit the created
column when the entity is saved?
Upvotes: 1
Views: 291
Reputation: 80593
Use a @PrePersist
handler to initialize the field. The initialization will be done prior to the entity being saved, thus avoiding the null pointer exception.
@PrePersist
protected void beforeCreate() {
if(created == null)
created = new Timestamp(System.currentTimeMillis());
}
Upvotes: 2