1

How does one auto generate the timestamp in a entity bean in hibernate?

Does the @Generator have anything to directly put in it like:

@GeneratedValue(vale=new Date().getTime())
Nico Huysamen
  • 10,217
  • 9
  • 62
  • 88

3 Answers3

1

You cannot put some code into the annotation, you just provide the values.

For automatically updating a timestamp at every change to the entity, you can use @Version on a timestamp field:

   @Version
   private Timestamp lastUpdate;
Alexander Rühl
  • 6,769
  • 9
  • 53
  • 96
1

You can implement global EntityInterceptor:

public class GlobalEntityInterceptor 
    extends EmptyInterceptor {
    @Override
    public boolean onSave(java.lang.Object entity, java.io.Serializable p2, java.lang.Object[] p3, java.lang.String[] p4, org.hibernate.type.Type[] p5) {   
  //first save - you can modify your entity fields
}
    @Override
    public boolean onFlushDirty(java.lang.Object entity, java.io.Serializable p2, java.lang.Object[] p3, java.lang.Object[] p4, java.lang.String[] p5, 
                                    org.hibernate.type.Type[] p6) { 
  //modification  - you can modify your entity fields 

}

there are some annotation-driven solutions like @PrePersist, but they only work in few environments.

Community
  • 1
  • 1
Boris Treukhov
  • 17,493
  • 9
  • 70
  • 91
0

You could use hibernates interceptors for populating entities with some data ie. creation date etc.

Read here for more information about interceptors

Timii
  • 234
  • 4
  • 11