简体   繁体   中英

Set the creation date and the change date of an entity by default using jhipster

I'm trying to set the creation date and the date of the last change of an entity in jhipster by default. I'm using a MySQL Database.

My Java code for the entity:

  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "creation_date", nullable = false, updatable = false)
  @JsonIgnore
  private Instant creationDate = Instant.now();

  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "change_date", nullable = false)
  @JsonIgnore
  private Instant changeDate = Instant.now();

My Html component:

<td>{{project.creationDate | date:'YYYY-MM-DD'}}</td>
<td>{{project.changeDate | date:'YYYY-MM-DD'}}</td>

The data type in MySQL is "date". The fields in the MySQL table work fine. But the frontend (Angular) doesn't show the dates. Instead there are just an empty fields for the dates. I already tried some different formats and data types (LocalDate, Date).

Does someone know a possible solution for this problem?

The problem is: you have the @JsonIgnore annotation on that fields. This annotation will prevent the field from being serialized into JSON. Therefore, your dates are simply missing in the REST-result on the frontend.

To verify that, you may show the whole JSON-Object on the frontend or dig into the HTTP response that is sent by the server, eg with chrome dev tools or a programm like cURL...

And a hint for creation timestamps:

You could have a look at the JPA auditing stuff, eg here: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#auditing There are two annotations like @CreatedDate and @LastModifiedDate , which you can use to automatically set those auditing dates for you.

And as already mentioned: you should use LocalDate or ZonedDateTime instead of Instant .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM