Yuriy Shkinder
Yuriy Shkinder

Reputation: 9

How to save DATE at the MySQL column

I am building simple REST with Spring Boot, MySQL, Hibernate. Want to save current date (autogenerated) using Hibernate but everytime i test it in the PostMan i get Null

@Entity
@Table (name="purchase")
public class Purchase {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name; 
@Temporal(TemporalType.DATE)
@Column(name="createat")
private Date created;}
CREATE TABLE `purchase`.`purchase` (
`id` INT NOT NULL             AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`createat` DATE,
PRIMARY KEY (`id`));

I Need to save current date to my createat COLUMN

Upvotes: 1

Views: 1038

Answers (1)

Vikas Suryawanshi
Vikas Suryawanshi

Reputation: 522

Using Hibernate you can just use @CreationTimestamp to insert default date.

@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "create_date")
private Date createDate;

and @UpdateTimestamp to update the value if necessary

@UpdateTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "modify_date")
private Date modifyDate;

Upvotes: 2

Related Questions