Spring Data @CreatedDate注释对我不起作用

我正在研究使用Spring Data的项目。 我想使用@CreatedDate注释来填充creationTime字段,而不是使用@PreUpdate@PrePersist注释的方法(以这种方式完美地工作)。 当我使用@CreatedDate它只是将此字段留空。 我使用postgresql数据库。 文档不是很有帮助。

你知道我该怎么办呢? 谢谢!

 import org.springframework.data.annotation.CreatedDate; @Entity @Table(name = "software") public class Software implements Serializable { // ... @Column(name = "creation_time") @CreatedDate private Date creationTime; //... } 

我的applicationContext

                ${hibernate.dialect} ${hibernate.show_sql} ${hibernate.format_sql} ${hibernate.ejb.naming_strategy} ${hibernate.hbm2ddl.auto}        

我可能处于类似的情况,我希望Spring Data JPA @CreatedDate注释能够工作,但不需要在文档中另外描述的用户级审计。

为了使基于注释的审计工作,我不得不在我的项目中添加一个实现org.springframework.data.domain.AuditorAware 。 这很奇怪,因为你实际上似乎并没有使用你将要实现的getCurrentAuditor()方法返回的值; 我只是返回null

 public class NullAuditorBean implements AuditorAware { @Override public Object getCurrentAuditor() { return null; } } 

然后我需要在applicationContext一个条目中引用我的“null对象” AuditorAware实现来激活JPA审计。 我必须确保在指定jpa:repositories的行之前执行此操作。 这看起来像:

   

我还必须添加一个orm.xml文件,并且需要正式引用它作为我的entityManagerFactory bean的属性,如下所示:

  META-INF/orm.xml  

确保此META-INF/orm.xml条目与您的编译输出一起存储(我的WAR在WEB-INF/classes下的WAR中。

对于记录,该orm.xml文件包含一些样板文件,可以在相关问题的答案中找到。

当我开始工作时,这是相当多的工作。 您可能更喜欢以前的工作解决方案!

这个问题很古老,但仍然相关。 对我来说,关键是这个,来自文档

从Spring Data开始,可以通过使用@EnableMongoAuditing批注对配置类进行批注来启用MongoDB 1.4审计。

例如:

 @Configuration @EnableMongoAuditing class Config { /** * Optional, depending on your needs */ @Bean public AuditorAware myAuditorProvider() { return new AuditorAwareImpl(); } } 

或者,在XML中: