0

I have a JPA entity where there is an updatedTime of type LocalDateTime column. I want to save it automatically through JPA with current time stamp , without setting any value in the entity object while saving. I looked at examples using Temporal annotations and also using column definition, but the second one saves it with null while the first one says this is allowed on type Date only. Can some please help me here? Note that I don't want to use triggers.

References: Setting a JPA timestamp column to be generated by the database?

http://www.java2s.com/Tutorials/Java/JPA/0160__JPA_Date_Column_Definition.htm

I have tried using using attribute converter as well, still the column is null. This is how the column is defined:

It doesn't work with the attribute converter either. This is how I have defined the column in entity: @Column(name="UPDATED_AT" , nullable=false,updatable=false, insertable=false, columnDefinition="TIMESTAMP DEFAULT CURRENT_TIME"

private LocalDateTime updatedTs;
user1318369
  • 715
  • 5
  • 15
  • 34
  • You must use an attribute converter, as can be see in https://thoughts-on-java.org/persist-localdate-localdatetime-jpa/ – Mihai8 Jun 26 '19 at 23:01
  • It doesn't work with the attribute converter either. This is how I have defined the column in entity(updated the post with this as well): @Column(name="UPDATED_AT" , nullable=false,updatable=false, insertable=false, columnDefinition="TIMESTAMP DEFAULT CURRENT_TIME" – user1318369 Jun 27 '19 at 00:04
  • This seems database specific. What database are you using. – K.Nicholas Jun 27 '19 at 00:26
  • We are using DB2 database – user1318369 Jun 27 '19 at 00:29
  • Does anyone know what can be done to populate this field during save? – user1318369 Jun 27 '19 at 02:00

0 Answers0