views:

1052

answers:

1

hello good people! 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  A: 

This is not related to Hibernate per se. Your annotations as specified above tell Hibernate that the values are going to be generated by the database and thus need to be reloaded after entity is inserted / updated.

If that's the way you want to go with, you need to configure your database (by creating a trigger, for example) to populate date_created / last_modified columns as needed.

Another approach is to not mark those fields as generated and instead update them in your java code. If you're using JPA (via Hibernate EntityManager), it's rather trivial to do this via @PrePersist / @PreUpdate callback method:

@PreUpdate
@PrePersist
public void updateTimeStamps() {
    lastModified = new Date();
    if (dateCreated==null) {
      dateCreated = new Date();
    }
}
ChssPly76
@ChssPly76: why can't he have 2 current_timestamps in the same table, I don't know this to be a Hibernate limitation and from your answer you don't think so either. So it must be a business/dba limitation (though his use of "apparently I can't have..." sounds like he was surprised by that epiphany), what if he can't use EM then what could be his recourse.
non sequitor
Hibernate doesn't care; it's a database limitation - MySQL to be precise. I've no idea why they don't allow this; possibly because they thought that those timestamps would always be the same.
ChssPly76
For ms sql server, timestamp means something completely different from datetime and it doesn't make sense to have multiple.
ashirley