Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Absence of @Temporal annotation in hibernate

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?

like image 223
Nandkumar Tekale Avatar asked Dec 12 '11 11:12

Nandkumar Tekale


People also ask

Which annotation is used to force Hibernate to store years months and days?

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.

What is the use of @temporal annotation?

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.

What is TIMESTAMP in hibernate?

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.


1 Answers

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.

like image 55
Tomasz Nurkiewicz Avatar answered Oct 14 '22 04:10

Tomasz Nurkiewicz