Rohit Patnaik
Rohit Patnaik

Reputation: 11

@CreationTimestamp in Hibernate is storing wrong date in database

I have used hibernate @CreationTimestamp (import org.hibernate.annotations.CreationTimestamp;) in my spring boot project for the field private Date createdDate; and Date is of import java.util.Date; to store the default current timestamp. This date is not having any setter method in java code.

I am using PostgreSQL 9.5.4 version and when I save, the created date is saved as 28th October 2022 but the correct created date which should have been stored is 23rd January 2023. But @UpdateTimestamp is working correctly. The picture from the database depicts the same.

enter image description here

@Entity
@Audited
public class EntityClassName implements Serializable{
    private static final long serialVersionUID = 6004021640401026397L;
    ...
    private String createdBy;
    
    @CreationTimestamp
    private Date createdDate;
    
    private String updatedBy;
    
    @UpdateTimestamp
    private Date updatedDate;

}

Update 1: So when I use @Temporal(TemporalType.TIMESTAMP) it stores the correct date in the database. Please find below code snippet:

@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
private Date createdDate;

But in the same database schema for other tables, I haven't used @Temporal(TemporalType.TIMESTAMP) and it is still working fine and storing the correct date in the database.

Can someone please guide what is going wrong here? Appreciate your help

Upvotes: 1

Views: 739

Answers (0)

Related Questions