How to instruct hibernate to set DB datetime for an entity attribute(s) automatically?

Use GenerationTime to instruct hibernate to set DB date time for an attribute.

E.g.

 @Column( nullable = false) 
    @Temporal( value = TemporalType.TIMESTAMP ) 
    @org.hibernate.annotations.Generated(value=GenerationTime.INSERT) 
    private Date crtdttm; 

 @Column( nullable = false ) 
    @Temporal( value = TemporalType.TIMESTAMP ) 
    @org.hibernate.annotations.Generated(value=GenerationTime.ALWAYS) 
    private Date upddttm; 

GenerationTime.INSERT – Set DB timestamp while Inserting only.

GenerationTime.ALWAYS – Set DB timestamp ALWAYS for all DML operations.

Advertisements

One Response to How to instruct hibernate to set DB datetime for an entity attribute(s) automatically?

  1. hector says:

    hey thanks for this. saved me a lot of research time!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: