Java Reference
In-Depth Information
The setBirthday() method can automatically calculate the age . The application does
this for convenience, but the age isn't something you want JPA to manage so you mark it
as @ Transient .
The @ Column attribute is the starting point for mapping your Java object properties to
database table columns. But JPA comes with a few more annotations to handle specific data
requirements, such as primary keys, dates, timestamps, codes, and binary data. We'll start
looking at examples of these kinds of data next.
9.3.4. Temporal types
Temporal types are all about dates and times. Most databases support a few different
temporal data types with different granularity levels corresponding to DATE (storing day,
month, and year), TIME (storing just time and not day, month, or year), and TIMESTAMP
(storing time, day, month, and year). The @Temporal annotation specifies which of these
data types you want.
JPA can map database data to either a java.util.Date or java.util.Calendar
object property. When saving the data to the database, JPA will use only the relevant parts
of the data stored in the property. This means if the database column is specified to store
only time, then JPA will get the time out of the java.util.Date or java.util
.Calendar property and ignore any date it holds.
JPA can also map to the java.sql.Date , java.sql.Time or
java.sql.Timestamp Java types. Using these with @Temporal is a bit redundant
because JPA will infer from the Java data type what kind of temporal data the database
stores. It doesn't hurt to be explicit, though, and using the @Temporal annotation makes
it clear how the data is to be handled.
As an example, suppose you store the date only, not the time, when data is created in the
database. You can do this in either of the following ways:
@Temporal(TemporalType.DATE)
protected java.util.Date creationDate;
@Temporal(TemporalType.DATE)
protected java.util.Calendar creationDate;
Search WWH ::




Custom Search