Why is it necessary to annotate the rendered Date fields @Temporal in javax.persistence?

The format looks like this:

@Temporal(TemporalType.DATE)
@Column(name="CREATED_DATE")
private Date createdDate;

      

My question is, why do I need to annotate the displayed fields java.util.Date

as @Temporal

in javax.persistence

? If the local variable is explicitly declared as a date, and the data type of the column in the DB is also one of the date (time) or timestamp types, it shouldn't be easy to conclude that we are dealing with a temporary data bit without redundantly specifying it in several places?

+3


source to share


4 answers


From for example java.util.Date it is not obvious if you want to map the database type to DATE or TIMESTAMP. The only exception is java.sql.Date/Time.



+5


source


If you want to create your database tables from jpa annotated code (on server startup, etc.) the @Temporal annotation is needed



+1


source


On the wikipedia page this answered me:

"If you map the Java type java.sql.Date to the DATE of the database, it is just a basic mapping and you should have no problem (disregard the Oracle DATE type, which is a timestamp at this point). Also map java .sql.Time to TIME and java.sql.Timestamp to TIMESTAMP. However, if you have java.util.Date or java.util.Calendar in Java and want to map it to DATE or TIME, you may need to specify that the provider JPA does some sort of conversion for this. In JPA it uses the annotation or @Temporal element to map this. You can specify that only the DATE or TIME part of the date / time will be stored in the database You can also use Temporal to map java.sql. Date with a TIMESTAMP field or any such conversion.

So you need @Temporal as a conversion level between your java type and target database

0


source


If you want to save java.util.Date

to a database, there are three ways to do this:

1- store only date ( java.sql.Date

): year / month / day

2- storage time and time ( java.sql.Time

): date + home / minute / second

3- storage timestamp ( java.sql.Timestamp

): date + time + nanoseconds

With the provision, @Temporal

you need to choose TemporalType.DATE

, TemporalType.TIME

or TemporalType.TIMESTAMP

, to determine the accuracy of yourjava.util.Date

0


source







All Articles