Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is @Temporal preferred to @Column columnDefinition?

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 ...

like image 586
Paul McKenzie Avatar asked Jul 19 '11 08:07

Paul McKenzie


People also ask

What is the use of @temporal annotation?

We use @Temporal annotation to insert date, time or both in database table. Using TemporalType we can insert data, time or both int table.

What is columnDefinition in @column annotation?

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.

What is@ column annotation?

The @Column annotation is used to specify the details of the column to which a field or property will be mapped.


1 Answers

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.

like image 130
Sean Patrick Floyd Avatar answered Sep 23 '22 08:09

Sean Patrick Floyd