0

I have created an abstract entity class to have only audit fields viz. createdDate and modifiedDate. I want hibernate to automatically generate values for these fields of type java.lang.Long when respective queries are fired.

I am trying to use @CreationTimestamp (org.hibernate.annotations.CreationTimestamp) but spring complains with following--

Invocation of init method failed; nested exception is org.hibernate.HibernateException: Unsupported property type for generator annotation @CreationTimestamp

I have also tried @Temporal but that does not work with fields of type Long. I don't want to store date in java.util.Date type.

Please help.

shankulk
  • 534
  • 1
  • 4
  • 23

1 Answers1

0

If you are using JPA you can do it like this

private Long createdDate;
private Long updatedDate;

@PrePersist
protected void onCreate() {
    createdDate = System.currentTimeMillis();
    updatedDate = System.currentTimeMillis();
}

@PreUpdate
protected void onUpdate() {
    updatedDate = System.currentTimeMillis();
}

Having said that why don't you want an updatedDate to be a Date??? I would think about making it a Date, not a Long.

See Creation timestamp and last update timestamp with Hibernate and MySQL

Community
  • 1
  • 1
Essex Boy
  • 7,565
  • 2
  • 21
  • 24
  • Thank you for quick response. I had already this seen this solution however I am using Session and not EntityManager. I want something that executes on `Session#save` and `Session#merge` – shankulk Jul 29 '16 at 10:07