简体   繁体   English

简单的Spring @CreatedDate注释对我不起作用

[英]Simple Spring @CreatedDate annotation is not working for me

I am trying to run a simple example as below but getting an error. 我试图运行一个简单的例子如下,但得到一个错误。

I am using @CreatedDate. 我正在使用@CreatedDate。 However, when I save Student object using a Rest endpoint, it does not create a date and throws the given exception. 但是,当我使用Rest端点保存Student对象时,它不会创建日期并抛出给定的异常。

In my Rest end point, I have 在我的休息终点,我有

 @PostMapping("/students")
 public Student createStudent(@Valid @RequestBody Student student){
    return studentRepository.save(student);
 }

Here is my entity... 这是我的实体......

@Entity
@Table(name = "students")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"createdAt", "updatedAt"}, allowGetters = true)
public class Student implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @NotBlank
    private String name;
    @NotBlank
    private String surname;

    @Column(nullable = false, updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    @CreatedDate
    private Date createdAt;

    @Column(nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    @LastModifiedDate
    private Date updatedAt;

Error: 错误:

2018-06-20 21:43:40.821 DEBUG 8844 --- [nio-8080-exec-1] org.hibernate.SQL                    

    : select next_val as id_val from hibernate_sequence for update
2018-06-20 21:43:40.874 DEBUG 8844 --- [nio-8080-exec-1] org.hibernate.SQL                        : update hibernate_sequence set next_val= ? where next_val=?
2018-06-20 21:43:40.971 DEBUG 8844 --- [nio-8080-exec-1] org.hibernate.SQL                        : insert into students (created_at, name, surname, updated_at, id) values (?, ?, ?, ?, ?)
2018-06-20 21:43:40.978 TRACE 8844 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [TIMESTAMP] - [null]
2018-06-20 21:43:40.979 TRACE 8844 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [VARCHAR] - [Shilpa]
2018-06-20 21:43:40.980 TRACE 8844 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [VARCHAR] - [Manware]
2018-06-20 21:43:40.980 TRACE 8844 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [4] as [TIMESTAMP] - [null]
2018-06-20 21:43:40.981 TRACE 8844 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [5] as [BIGINT] - [3]
2018-06-20 21:43:41.003  WARN 8844 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1048, SQLState: 23000
2018-06-20 21:43:41.003 ERROR 8844 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : Column 'created_at' cannot be null
2018-06-20 21:43:41.006  INFO 8844 --- [nio-8080-exec-1] o.h.e.j.b.internal.AbstractBatchImpl     : HHH000010: On release of batch it still contained JDBC statements
2018-06-20 21:43:41.009 ERROR 8844 --- [nio-8080-exec-1] o.h.i.ExceptionMapperStandardImpl        : HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]
2018-06-20 21:43:41.041 ERROR 8844 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'created_at' cannot be null
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_112]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_112]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_112]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_112]
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.45.jar:5.1.45]
    at com.mysql.jdbc.Util.getInstance(Util.java:408) ~[mysql-connector-java-5.1.45.jar:5.1.45]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936) ~[mysql-connector-java-5.1.45.jar:5.1.45]
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973) ~[mysql-connector-java-5.1.45.jar:5.1.45]
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909) ~[mysql-connector-java-5.1.45.jar:5.1.45]
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527) ~[mysql-connector-java-5.1.45.jar:5.1.45]

您可能会错过Configuration类中的@EnableJpaAuditing注释,该注释在Spring JPA中启用审计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 数据@CreatedDate 注释对我不起作用 - Spring Data @CreatedDate annotation doesn't work for me Spring Data Couchbase @CreatedDate 不起作用 - Spring Data Couchbase @CreatedDate not working 手动分配 ID 时,Spring Data MongoDB Annotation @CreatedDate 不起作用 - Spring Data MongoDB Annotation @CreatedDate isn't working, when ID is assigned manually @CreatedDate 注解不适用于 spring-data-elasticsearch - @CreatedDate annotation does not work with spring-data-elasticsearch Spring Mongo 审计不工作@CreatedDate @CreatedBy - Spring Mongo Auditing not working @CreatedDate @CreatedBy @CreatedDate 注释不适用于 mysql - @CreatedDate annotation does not work with mysql Spring Data MongoDB - 使用自定义 Id 字段时,注释 @CreatedDate 不起作用 - Spring Data MongoDB - Annotation @CreatedDate does not work while using with custom Id field spring jpa auditing lastmodifiedby和lastmodifiedDate都可以,但createdBy和createdDate注释总是为null - spring jpa auditing lastmodifiedby and lastmodifiedDate are ok, but createdBy and createdDate annotation are always null Spring AOP 注释不起作用 - Spring AOP Annotation not working 自定义Spring注释不起作用 - Custom Spring annotation not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM