在 Spring Boot with JPA 中,如何配置我的应用程序以便日期列自动填充其创建的当前时间?
In Spring Boot with JPA, how do I configure my application so that a date column auto-populates with the current time of it's creation?
我正在使用 Spring Boot 2 和 Java 11。我创建了以下 JPA 实体 ...
@Entity
@Table(name = "Mailings")
public class Mailing {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private UUID id;
@ManyToOne
@NotNull
private Card card;
private String message;
@Column(columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
private java.sql.Timestamp mailingDate;
@OneToOne(cascade = CascadeType.ALL)
@NotNull
private Address senderAddress;
@OneToOne(cascade = CascadeType.ALL)
@NotNull
private Address recipientAddress;
}
这是在我启动开发应用程序时在我的 PostGres 10 数据库中正确创建的...
cardmania=# \d mailings;
Table "public.mailings"
Column | Type | Modifiers
----------------------+-----------------------------+---------------
id | uuid | not null
creation_date | timestamp without time zone | default now()
message | character varying(255) |
card_id | uuid | not null
recipient_address_id | uuid | not null
sender_address_id | uuid | not null
Indexes:
"mailings_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
"fk5hy4mbv0ewd82t5b8b7shaxkc" FOREIGN KEY (sender_address_id) REFERENCES addresses(id)
"fk67j1xe6kw5510en1daylstnnn" FOREIGN KEY (card_id) REFERENCES cards(id)
"fknwiu32uusnxegnulcj1di158k" FOREIGN KEY (recipient_address_id) REFERENCES addresses(id)
然后我创建了这个控制器来处理 POST 请求...
@RestController
@RequestMapping("/api/mailing")
public class MailingController {
@Autowired
private MailingService mailingService;
@PostMapping
@ResponseStatus(code = HttpStatus.CREATED)
public void create(@Valid @RequestBody Mailing mailing) {
mailingService.save(mailing);
}
}
但是我面临的一个问题是,当我使用 JSON 提交 POST 请求时,如下所示
{
"senderAddress": {
"name": "Joe Recipient",
"city": "Los Angeles",
"state": "California",
"zip_code": "60615",
"street": "555 Hello Way"
},
"recipientAddress": {
"name": "Joe Recipient",
"city": "Los Angeles",
"state": "California",
"zip_code": "60615",
"street": "555 Hello Way"
},
"card": {
"id": "05b7af7c-1de5-4a72-aebf-4c9e4d9acec3"
},
"message": "Hi"
}
实体在我的数据库中正确创建,但“creation_date”字段为空,而不是填充当前时间戳。请注意,该列是使用“default now()”修饰符生成的,那么我还需要做什么才能正确填充我的时间戳列?
如果只想让列填充其创建时间戳,您还可以使用@CreationTimestamp:
@CreationTimestamp
private Date mailingDate;
我正在使用 Spring Boot 2 和 Java 11。我创建了以下 JPA 实体 ...
@Entity
@Table(name = "Mailings")
public class Mailing {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private UUID id;
@ManyToOne
@NotNull
private Card card;
private String message;
@Column(columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
private java.sql.Timestamp mailingDate;
@OneToOne(cascade = CascadeType.ALL)
@NotNull
private Address senderAddress;
@OneToOne(cascade = CascadeType.ALL)
@NotNull
private Address recipientAddress;
}
这是在我启动开发应用程序时在我的 PostGres 10 数据库中正确创建的...
cardmania=# \d mailings;
Table "public.mailings"
Column | Type | Modifiers
----------------------+-----------------------------+---------------
id | uuid | not null
creation_date | timestamp without time zone | default now()
message | character varying(255) |
card_id | uuid | not null
recipient_address_id | uuid | not null
sender_address_id | uuid | not null
Indexes:
"mailings_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
"fk5hy4mbv0ewd82t5b8b7shaxkc" FOREIGN KEY (sender_address_id) REFERENCES addresses(id)
"fk67j1xe6kw5510en1daylstnnn" FOREIGN KEY (card_id) REFERENCES cards(id)
"fknwiu32uusnxegnulcj1di158k" FOREIGN KEY (recipient_address_id) REFERENCES addresses(id)
然后我创建了这个控制器来处理 POST 请求...
@RestController
@RequestMapping("/api/mailing")
public class MailingController {
@Autowired
private MailingService mailingService;
@PostMapping
@ResponseStatus(code = HttpStatus.CREATED)
public void create(@Valid @RequestBody Mailing mailing) {
mailingService.save(mailing);
}
}
但是我面临的一个问题是,当我使用 JSON 提交 POST 请求时,如下所示
{
"senderAddress": {
"name": "Joe Recipient",
"city": "Los Angeles",
"state": "California",
"zip_code": "60615",
"street": "555 Hello Way"
},
"recipientAddress": {
"name": "Joe Recipient",
"city": "Los Angeles",
"state": "California",
"zip_code": "60615",
"street": "555 Hello Way"
},
"card": {
"id": "05b7af7c-1de5-4a72-aebf-4c9e4d9acec3"
},
"message": "Hi"
}
实体在我的数据库中正确创建,但“creation_date”字段为空,而不是填充当前时间戳。请注意,该列是使用“default now()”修饰符生成的,那么我还需要做什么才能正确填充我的时间戳列?
如果只想让列填充其创建时间戳,您还可以使用@CreationTimestamp:
@CreationTimestamp
private Date mailingDate;