@CreatedDate带注释的字段未写在Insert上,@ LastModifiedDate是 [英] @CreatedDate annotated field is not written on Insert, @LastModifiedDate is

查看:326
本文介绍了@CreatedDate带注释的字段未写在Insert上,@ LastModifiedDate是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了以下实体并使用h2对其进行了测试:

  @Getter 
公共类Topic {

@Id
私人长ID;

私人最终标题标题;

@CreatedDate
private LocalDateTime createdAt;

LastModifiedDate
私有LocalDateTime lastModified;

// ...
}

The TopicRepository 是一个空接口。



以下测试失败,并显示错误 createdAt 为空:

  @RunWith(SpringRunner.class)
@SpringBootTest
公共类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);
assertNotNull(savedTopic.getCreatedAt(),必须设置createdAt);

topicRepository.delete(savedTopic);

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

}

我的应用程序带有<$注释c $ c> @SpringBootApplication 和 @EnableJdbcAuditing



为什么 createdAt 仍然为 null ,另一方面, lastModified 是否不为空? / p>

编辑



我更改了 Topic.createdAt 和 Topic.lastModified Instant 均无效,



我还添加了以下方法,我想应该为 Instant 字段提供值:

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

不幸的是,尽管调用了该方法, createdAt 仍然为 null

解决方案

审核注释只考虑聚合根。如果属于聚合而不是聚合根的实体需要审计信息,可以通过在聚合根中实现该信息来实现,该信息应管理对其和聚合实体的所有更改。



问题中发布的源代码表明您实际上正在查看聚合根,而通过Github提供的代码表明,根上的注释可以正常运行,但非根实体上的注释可以正常运行'



您不需要 AuditorAware bean。
只有 @CreatedBy @LastModifiedBy 才需要。


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.

解决方案

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.

这篇关于@CreatedDate带注释的字段未写在Insert上,@ LastModifiedDate是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆