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

2

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, that @CreationTimestamp was exactly what I needed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.