【问题标题】:@CreatedDate annotation does not work with mysql@CreatedDate 注解不适用于 mysql
【发布时间】:2017-03-15 05:17:04
【问题描述】:

我是 spring 新手,我对 @CreatedDate 注释在实体中的工作方式感到困惑。

我做了一个谷歌搜索,有很多解决方案,但除了一个之外,没有一个对我有用。我很困惑为什么?

这是我第一次尝试的

@Entity
@EntityListeners(AuditingEntityListener.class)
public class User implements Serializable {

    @Id
    @GeneratedValue
    private Long id;
    private String name;

    @CreatedDate
    private Date created;

    public User(String name) {

        this.name = name;
    }

    public User() {
    }

它没有工作。我得到了 created 列中的值的 NULL。

然后我就这样做了。

@Entity
@EntityListeners(AuditingEntityListener.class)
public class User implements Serializable {

    @Id
    @GeneratedValue
    private Long id;
    private String name;

    @CreatedDate
    private Date created = new Date();

    public User(String name) {

        this.name = name;
    }

    public User() {
    }

这实际上将时间戳存储在数据库中。我的问题是我遵循的大多数教程都建议我不需要new Date() 来获取当前时间戳。看来我确实需要那个。有什么我想念的吗?

【问题讨论】:

标签: java mysql spring-boot spring-data-jpa audit-tables


【解决方案1】:

我也遇到了这个问题,你的解决方案帮助了我,谢谢,我添加了一些其他注释来工作

首先确保你放入 SpringApplication 配置

@SpringBootApplication
@EnableJpaAuditing

其次,确保你在你需要的实体上使用这个注解

  @Entity
  @Table
  @EntityListeners(AuditingEntityListener.class)

【讨论】:

  • 谢谢,这帮助我解决了 @CreatedDate 注释不起作用的问题。不建议将此注释与 @SpringBootApplication 一起使用。测试时出现以下错误:java.lang.IllegalArgumentException: JPA metamodel must not be empty!。请仔细阅读this 答案。应该确保不相关的切片在测试时也不会干扰。
【解决方案2】:

如果您只是将@EntityListeners(AuditingEntityListener.class) 放在您的实体上,@CreatedDate 将无法单独工作。为方便起见,您必须进行更多配置。

假设在您的数据库中@CreatedDate 的字段是String 类型,并且您希望将当前登录的用户作为@CreatedDate 的值返回,然后执行以下操作:

public class CustomAuditorAware implements AuditorAware<String> {

    @Override
    public String getCurrentAuditor() {
        String loggedName = SecurityContextHolder.getContext().getAuthentication().getName();
        return loggedName;
    }

}

您可以在那里编写任何适合您需要的功能,但您当然必须有一个引用实现 `AuditorAware 的类的 bean

同样重要的第二部分是创建一个 bean,它返回带有 @EnableJpaAuditing 注释的类,如下所示:

@Configuration
@EnableJpaAuditing
public class AuditorConfig {

    @Bean
    public CustomAuditorAware auditorProvider(){
        return new CustomAuditorAware();
    }
}

如果你的毒药是 XML 配置,那么这样做:

<bean id="customAuditorAware" class="org.moshe.arad.general.CustomAuditorAware" />
    <jpa:auditing auditor-aware-ref="customAuditorAware"/>

【讨论】:

  • 我有相同的配置,但这会将值设置为 @CreatedBy@LastModifiedBy 注释字段。注释为@LastModifiedDate@CreatedDate 的其他字段始终为空。如何解决这个问题?
  • 您是否尝试过这里所说的:stackoverflow.com/a/50948632/986160
【解决方案3】:

您可以像这样使用@creationTimestamp 和@UpdateTimestamp:

  @CreationTimestamp
  private Instant createdAt;

  @UpdateTimestamp
  private Instant updatedAt;

【讨论】:

    【解决方案4】:

    如果你想使用java8时间类:

    @EnableJpaAuditing(dateTimeProviderRef = "auditingDateTimeProvider")
    ....
    
    @Bean(name = "auditingDateTimeProvider")
    public DateTimeProvider dateTimeProvider() {
            return () -> Optional.of(OffsetDateTime.now());
    }
    

    【讨论】:

      【解决方案5】:

      如果不使用 JPA 实体侦听器,创建日期将不起作用,因为这是 JPA 用于审计和创建或修改日期的方式。

      只需修改如下代码:

      @Entity
      @EntityListeners(AuditingEntityListener.class)
      class XYZ {
      
          @Setter(value = AccessLevel.NONE)
          @CreatedDate
          @Column(name = "date_time")
          private Date dateTime;
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 2021-07-17
        • 2020-02-20
        • 1970-01-01
        • 2021-02-27
        • 1970-01-01
        • 2014-08-12
        • 2019-10-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多