Spring Boot JPA@CreatedDate @LastModifiedDate not being populated when saving the object

后端 未结 4 1172
南笙
南笙 2021-01-17 23:21

I am writing an app with Spring Boot + JPA, using a Postgres database. I have a User Entity and I am trying to get a timestamp when the user record is saved and/or modified.

4条回答
  •  隐瞒了意图╮
    2021-01-17 23:48

    The AuditingEntityListener methods are called in the @PrePersist and @PreUpdate phase.

    This means they are called just before the insert or update SQL statements are executed.

    Read more about JPA events in the Hibernate doc: https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#events-jpa-callbacks

    Unit Tests

    When using in Tests you have to enable auditing as well on the test

    @DataJpaTest
    @RunWith(SpringRunner.class)
    @EnableJpaAuditing
    public class EntityListenerTest {
    

提交回复
热议问题