@CreatedDate annotated field is not written on Insert, @LastModifiedDate is

為{幸葍}努か 提交于 2020-01-06 06:36:14

问题


I created the following Entity and test it using h2:

@Getter
public class Topic {

    @Id
    private long id;

    private final Title title;

    @CreatedDate
    private LocalDateTime createdAt;

    @LastModifiedDate
    private LocalDateTime lastModified;

    // ...
}

The TopicRepository is an empty interface.

The following test fails with the error that createdAt is null:

@RunWith(SpringRunner.class)
@SpringBootTest
public class BasicRepositoryTests {

    @Autowired
    TopicRepository topicRepository;

    @Test
    public void topicRepositoryWorks() {
        val topic = new Topic();
        val savedTopic = topicRepository.save(topic);

        assertEquals(1, topicRepository.count());
        assertNotNull(savedTopic.getLastModified(), "lastModified must be set");
        assertNotNull(savedTopic.getCreatedAt(), "createdAt must be set");

        topicRepository.delete(savedTopic);

        assertEquals(0, topicRepository.count());
    }

}

My Application is annotated with @SpringBootApplication and @EnableJdbcAuditing.

Why is createdAt still null, lastModified on the other hand is not null?

Edit

I changed the types of Topic.createdAt and Topic.lastModified to Instant, which did not work.

Also, I added the following method, which, I guess, should provide values for the Instant fields:

@Bean
public AuditorAware<Instant> instantAuditorAware() {
    return () -> Optional.of(Instant.now());
}

Sadly, although the method gets called, createdAt is still null.


回答1:


Auditing annotations get only considered for the aggregate root. If auditing information is needed for entities that are part of an aggregate but not the aggregate root this can be done by implementing it in the aggregate root which should manage all changes to it and the entities of the aggregate.

While the source code posted in the question suggests that you are actually looking at the aggregate root the code you made available via Github shows that the annotation on the root works fine but on the non root entity doesn't as described above.

You don't need an AuditorAware bean. That is only needed for @CreatedBy and @LastModifiedBy.



来源:https://stackoverflow.com/questions/56542700/createddate-annotated-field-is-not-written-on-insert-lastmodifieddate-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!