@CreatedDate annotated field is not written on Ins

2019-08-27 16:39发布

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条回答
Lonely孤独者°
2楼-- · 2019-08-27 17:25

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.

查看更多
登录 后发表回答