java @CreatedDate 注释不适用于 mysql

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40370709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:10:09  来源:igfitidea点击:

@CreatedDate annotation does not work with mysql

javamysqlspring-bootspring-data-jpaaudit-tables

提问by Fawzan

I am new to spring and I am confused how @CreatedDate annotation works in an entity.

我是 spring 的新手,我很困惑 @CreatedDate 注释如何在实体中工作。

I did a google search and there were many solutions, but none of them worked for me except one. I am confused why?

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

This is what I tried first

这是我首先尝试的

@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() {
    }

It did not work. I got NULL for the value in createdcolumn.

这没用。我为created列中的值获得了 NULL 。

Then I did this.

然后我做了这个。

@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() {
    }

This actually stored the time stamp in the db. My question is most of the tutorials I followed suggested that I do not need new Date()to get the current time stamp. Looks like I do need that. Is there anything I am missing?

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

回答by Moshe Arad

The @CreatedDatewon't work by itself if you just put @EntityListeners(AuditingEntityListener.class)on your entities. In order, it'll work you have to do a little more configuration.

@CreatedDate,如果你只是把本身不会工作@EntityListeners(AuditingEntityListener.class)在你的实体。为了,它会工作你必须做更多的配置。

Let's say that in your DB the field of @CreatedDateis String type, and you want to return the user that is currently logged in as a value for @CreatedDate, then do this:

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

public class CustomAuditorAware implements AuditorAware<String> {

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

}

You can write there any functionality that fits your needs, but you certainly must have a bean that reference to a class that implements `AuditorAware

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

The second part and equally important, is to create a bean that returns that class with annotation of @EnableJpaAuditing, like this:

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

@Configuration
@EnableJpaAuditing
public class AuditorConfig {

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

if your poison is XML configuration then do this:

如果您的毒药是 XML 配置,请执行以下操作:

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

回答by Ismail

I was Having this issue also and your solution helped me, Thanks, And I add some other annotations to work

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

Fist make sure you put in SpringApplication Configuration

拳头确保你放入了 SpringApplication 配置

@SpringBootApplication
@EnableJpaAuditing

Second, make sure you use this annotation on your needed entities

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

  @Entity
  @Table
  @EntityListeners(AuditingEntityListener.class)