hard time setting autogenerated time with hibernate JPA annotations

Viewed 22232

thanks to you guys my knowlegde on hibernate has been improve dratiscally. now i hit a block here about current_timestamp. here is my codes

@Column(name="DATE_CREATED", insertable=false, updatable=false, columnDefinition="timestamp default current_timestamp")
@org.hibernate.annotations.Generated(value=GenerationTime.INSERT)
@Temporal(javax.persistence.TemporalType.TIMESTAMP)
private Date dateCreated;

@Column(name="LAST_MODIFIED", insertable=false, updatable=false, columnDefinition="datetime")
@org.hibernate.annotations.Generated(value=GenerationTime.ALWAYS)
@Temporal(javax.persistence.TemporalType.TIMESTAMP)
private Date lastModified;

i want date_created to get the current_timestamp and i want the lastmodified to insert the time for each updates.apparently i can't have 2 current_timestamp fields on the same table.Is there other ways to achieve that? thanks for reading

2 Answers

You could achieve the same thing with hibernates @CreationTimestampand @UpdateTimestamp annotations e.g.

@Column(name = "CREATED")
@CreationTimestamp
private LocalDateTime created;

@Column(name = "LAST_UPDATED")
@UpdateTimestamp
private LocalDateTime lastUpdated;
Related