1

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

  • You can add a default value in Java with `private Date created = new Date();`, I'm not sure this is what you were asking for. – vicpermir May 29 '19 at 07:31
  • Possible duplicate of [How can I use DB side default value while use Hibernate save?](https://stackoverflow.com/questions/14703697/how-can-i-use-db-side-default-value-while-use-hibernate-save) – Joakim Danielson May 29 '19 at 08:13

1 Answers1

2

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;
Vikas Suryawanshi
  • 522
  • 1
  • 5
  • 12