Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Basic Mappings/Temporal"
(→@Temporal) |
|||
(6 intermediate revisions by the same user not shown) | |||
Line 10: | Line 10: | ||
=@Temporal= | =@Temporal= | ||
− | Use the <tt>@Temporal</tt> annotation to specify the database type that EclipseLink persistence provider should persist for persistent fields or properties of type <tt>java.util.Date</tt> and <tt>java.util.Calendar</tt> only. | + | Use the <tt>@Temporal</tt> annotation or <code><temporal></code> XML to specify the database type that EclipseLink persistence provider should persist for persistent fields or properties of type <tt>java.util.Date</tt> and <tt>java.util.Calendar</tt> only. |
You can use this annotation with the [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings|<tt>@Basic</tt>]] annotation. | You can use this annotation with the [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Basic_Mappings|<tt>@Basic</tt>]] annotation. | ||
Line 20: | Line 20: | ||
<td>Set this attribute to the <tt>TemporalType</tt> that corresponds to database type you want EclipseLink persistence provider to use: | <td>Set this attribute to the <tt>TemporalType</tt> that corresponds to database type you want EclipseLink persistence provider to use: | ||
* <tt>DATE</tt> – equivalent of <tt>java.sql.Date</tt> | * <tt>DATE</tt> – equivalent of <tt>java.sql.Date</tt> | ||
− | * <tt>TIME</tt> – equivalent of <tt>java. | + | * <tt>TIME</tt> – equivalent of <tt>java.sql.Time</tt> |
* <tt>TIMESTAMP</tt> – equivalent of <tt>java.sql.Timestamp</tt></td> | * <tt>TIMESTAMP</tt> – equivalent of <tt>java.sql.Timestamp</tt></td> | ||
<td></td> | <td></td> | ||
Line 32: | Line 32: | ||
======'' Example: @Temporal Annotation''====== | ======'' Example: @Temporal Annotation''====== | ||
<source lang="java"> | <source lang="java"> | ||
− | + | @Entity | |
− | + | public class Employee implements Serializable { | |
− | + | ... | |
− | + | @Temporal(DATE) | |
− | + | protected java.util.Date startDate; | |
− | + | ... | |
− | + | } | |
+ | </source> | ||
+ | |||
+ | ======''Example: Using <code><nowiki><temporal></nowiki></code> XML''====== | ||
+ | <source lang="xml"> | ||
+ | <entity class="Employee"> | ||
+ | <attributes> | ||
+ | ... | ||
+ | <basic name="startDate"> | ||
+ | <temporal>DATE</temporal> | ||
+ | </basic> | ||
+ | ... | ||
+ | </attributes> | ||
+ | </entity> | ||
</source> | </source> | ||
Line 44: | Line 57: | ||
|section=Section 11.1.47 "Temporal Annotation" | |section=Section 11.1.47 "Temporal Annotation" | ||
}} | }} | ||
+ | |||
+ | ====Timezones and Calendars==== | ||
+ | In Java a <code>Calendar</code> also stores a timezone and day light saving offset in addition to the date and time values. Normally this timezone is lost when storing the <code>Calendar</code> to the database. Some databases have support for storing timezones and offsets, so it is possible to store these to the database as well. | ||
+ | |||
+ | EclipseLink supports storing timezones in an Oracle database through the <code>TIMESTAMPTZ</code> and <code>TIMESTAMPLTZ</code> types in Oracle. To store timezones, ensure the column is of type <code>TIMESTAMPTZ</code>, <code>Calendar</code> is used as the type in Java, and the <code>Oracle9Platform</code> or higher is used. You can also use a <code>TypeConverter</code> and set the <code>dataType</code> to <code>oracle.sql.TIMESTAMPTZ</code> or <code>oracle.sql.TIMESTAMPLTZ</code>. | ||
Latest revision as of 14:59, 27 October 2011
EclipseLink JPA
EclipseLink | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
Open • Help Wanted • Bug Day | |
Contribute | |
Browse Source |
Key API
@Temporal
Use the @Temporal annotation or <temporal>
XML to specify the database type that EclipseLink persistence provider should persist for persistent fields or properties of type java.util.Date and java.util.Calendar only.
You can use this annotation with the @Basic annotation.
Attribute | Description | Default | Required? |
---|---|---|---|
value | Set this attribute to the TemporalType that corresponds to database type you want EclipseLink persistence provider to use:
|
Yes |
The following example shows how to use this annotation to specify that EclipseLink persistence provider should persist java.util.Date field startDate as a DATE (java.sql.Date) database type.
Example: @Temporal Annotation
@Entity public class Employee implements Serializable { ... @Temporal(DATE) protected java.util.Date startDate; ... }
Example: Using <temporal>
XML
<entity class="Employee"> <attributes> ... <basic name="startDate"> <temporal>DATE</temporal> </basic> ... </attributes> </entity>
For more information, see Section 11.1.47 "Temporal Annotation" in the JPA Specification.
Timezones and Calendars
In Java a Calendar
also stores a timezone and day light saving offset in addition to the date and time values. Normally this timezone is lost when storing the Calendar
to the database. Some databases have support for storing timezones and offsets, so it is possible to store these to the database as well.
EclipseLink supports storing timezones in an Oracle database through the TIMESTAMPTZ
and TIMESTAMPLTZ
types in Oracle. To store timezones, ensure the column is of type TIMESTAMPTZ
, Calendar
is used as the type in Java, and the Oracle9Platform
or higher is used. You can also use a TypeConverter
and set the dataType
to oracle.sql.TIMESTAMPTZ
or oracle.sql.TIMESTAMPLTZ
.
@Enumerated | @Lob | |
EclipseLink Home
JPA User Guide: Table of Contents, Search |
||
How to contribute to this guide... |