Which is best practice?
@Column(name = "FOO", columnDefinition = "TIMESTAMP")
private Date foo;
Or
@Column(name = "FOO")
@Temporal(TemporalType.TIMESTAMP)
private Date foo;
The documentation suggests that using columnDefinition is non-portable ...
We use @Temporal annotation to insert date, time or both in database table. Using TemporalType we can insert data, time or both int table.
columnDefinition definition: The SQL fragment that is used when generating the DDL for the column. columnDefinition default: Generated SQL to create a column of the inferred type.
The @Column annotation is used to specify the details of the column to which a field or property will be mapped.
The documentation suggests that using columnDefinition is non-portable ...
That's true. columnDefinition
specified the SQL data type that will be used. This data type may however not be available in all RDBMS. In JPA, it's the JPA provider's duty to figure out what SQL works on what DB. You can specifiy part of that configuration, but you will always risk breaking support for some databases.
@Temporal
on the other hand is an abstraction that is part of the JPA standard. Every JPA provider must be able to map the different types of @Temporal
to different SQL types for all supported databases.
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