What if we use
@Column(name="birth_date", nullable=false, length=19) public Date getBirthDate() { return this.birthDate; }
instead of
@Temporal(TemporalType.TIMESTAMP) @Column(name="birth_date", nullable=false, length=19) public Date getBirthDate() { return this.birthDate; }
Are there any side effects if we use date column property without @Temporal
annotation?
You can define the preferred mapping with the @Temporal annotation. As you can see in the following code snippet, it takes a TemporalType enum as a value. The enum allows you to select the SQL type (DATE, TIME or TIMESTAMP) which you want to use.
Temporal data can have DATE, TIME, or TIMESTAMP precision (i.e., the actual date, only the time, or both). Use the @Temporal annotation to fine tune that. The temporal data is the data related to time. For example, in a content management system, the creation-date and last-updated date of an article are temporal data.
When you persist a new MyEntity, Hibernate will get the current time from the VM and store it as the creation and update timestamp. As you can see in the log output, Hibernate gets a new timestamp for each attribute. The creation and update timestamp will therefore not be the same even if the entity was never updated.
Only piece of documentation I managed to find:
In plain Java APIs, the temporal precision of time is not defined. When dealing with temporal data you might want to describe the expected precision in database. Temporal data can have DATE, TIME, or TIMESTAMP precision (ie the actual date, only the time, or both). Use the @Temporal annotation to fine tune that.
From 2.2.2.1. Declaring basic property mappings.
This might indicate that the actual date representation in the database is not defined and to be sure it is better to specify it directly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With