Set LocalDateTime to autogenerate with hibernate 4.3.10
I currently have a base object like this:
@MappedSuperclass
public abstract class BaseEntity {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
private Long id;
private boolean deleted;
@Convert(converter = LocalDateTimePersistenceConverter.class)
private LocalDateTime createdAt;
@Convert(converter = LocalDateTimePersistenceConverter.class)
private LocalDateTime updatedAt;
}
Is it possible to annotate LocalDateTime
something to make the default database current and temporary?
ps I am not allowed to use hibernation 5.
+3
source to share
2 answers
You can use @PrePersist annotation.
Executed before management of the entity is actually executed or cascaded. This call is synchronous with the persisting operation.
Example:
@PrePersist
protected void onCreate() {
createdAt = new LocalDateTime();
updatedAt = new LocalDateTime();
}
And if you deem it appropriate, you will also get @PreUpdate annotation.
More about events that happen inside hibernate persistence engine
+1
source to share
@LaurentiuL is correct.
But I think below should also work
@Convert(converter = LocalDateTimePersistenceConverter.class)
private LocalDateTime createdAt = new LocalDateTime ();
@Convert(converter = LocalDateTimePersistenceConverter.class)
private LocalDateTime updatedAt= new LocalDateTime ();
Also the answers to this question should help you: Creation timestamp and last update timestamp with Hibernate and MySQL
0
source to share