@@ -96,45 +96,73 @@ Similarly, the Java-style postfix is case-insensitive.
96
96
[[datetime-literals]]
97
97
==== Date and time literals
98
98
99
- According to the JPQL specification, date and time literals may be specified using the JDBC escape syntax.
100
- Since this syntax is rather unpleasant to look at, HQL provides not one, but two alternatives.
99
+ According to the JPQL specification, literal dates and times may be specified using the JDBC escape syntax.
101
100
101
+ [cols="~,~,~,^15"]
102
102
|===
103
- | Date/time type | Recommended Java type | JDBC escape syntax 💀| Braced literal syntax | Explicitly typed literal syntax
104
-
105
- | Date | `LocalDate` | `{d 'yyyy-mm-dd'}` | `{yyyy-mm-dd}` | `date yyyy-mm-dd`
106
- | Time | `LocalTime` | `{t 'hh:mm'}` | `{hh:mm}` | `time hh:mm`
107
- | Time with seconds | `LocalTime` | `{t 'hh:mm:ss'}` | `{hh:mm:ss}` | `time hh:mm:ss`
108
- | Datetime | `LocalDateTime` | `{ts 'yyyy-mm-ddThh:mm:ss'}` | `{yyyy-mm-dd hh:mm:ss}` | `datetime yyyy-mm-dd hh:mm:ss`
109
- | Datetime with milliseconds | `LocalDateTime` | `{ts 'yyyy-mm-ddThh:mm:ss.millis'}` | `{yyyy-mm-dd hh:mm:ss.millis}` | `datetime yyyy-mm-dd hh:mm:ss.millis`
110
- | Datetime with an offset | `OffsetDateTime` | `{ts 'yyyy-mm-ddThh:mm:ss+hh:mm'}` | `{yyyy-mm-dd hh:mm:ss +hh:mm}` | `datetime yyyy-mm-dd hh:mm:ss +hh:mm`
111
- | Datetime with a time zone | `OffsetDateTime` | `{ts 'yyyy-mm-ddThh:mm:ss GMT'}` | `{yyyy-mm-dd hh:mm:ss GMT}` | `datetime yyyy-mm-dd hh:mm:ss GMT`
103
+ | Date/time type | Java type | JDBC escape syntax | JPA standard
104
+
105
+ | Date | `java.sql.Date` 💀 | `{d 'yyyy-mm-dd'}` | ✔
106
+ | Time | `java.sql.Time` 💀 | `{t 'hh:mm'}` | ✔
107
+ | Time with seconds | `java.sql.Time` 💀 | `{t 'hh:mm:ss'}` | ✔
108
+ | Timestamp | `java.sql.Timestamp` 💀 | `{ts 'yyyy-mm-ddThh:mm:ss'}` | ✔
109
+ | Timestamp with milliseconds | `java.sql.Timestamp` 💀 | `{ts 'yyyy-mm-ddThh:mm:ss.millis'}` | ✔
110
+ | Timestamp with an offset | `java.sql.Timestamp` 💀 | `{ts 'yyyy-mm-ddThh:mm:ss+hh:mm'}` | ✔
111
+ | Timestamp with a time zone | `java.sql.Timestamp` 💀 | `{ts 'yyyy-mm-ddThh:mm:ss GMT'}` | ✔
112
112
|===
113
113
114
- Literals referring to the current date and time are also provided.
115
- Again there is some flexibility.
114
+ There's two problems here:
116
115
117
- |===
118
- | Date/time type | Java type | Underscored syntax | Spaced syntax
119
-
120
- | Date | `java.time.LocalDate` | `local_date` | `local date`
121
- | Time | `java.time.LocalTime` | `local_time` | `local time`
122
- | Datetime | `java.time.LocalDateTime` | `local_datetime` | `local datetime`
123
- | Offset datetime | `java.time.OffsetDateTime`| `offset_datetime` | `offset datetime`
124
- | Instant | `java.time.Instant` | `instant` | `instant`
125
- | Date | `java.sql.Date` 💀| `current_date` | `current date`
126
- | Time | `java.sql.Time` 💀| `current_time` | `current time`
127
- | Datetime | `java.sql.Timestamp` 💀| `current_timestamp` | `current timestamp`
128
- |===
129
-
130
- Of these, only `local date`, `local time`, `local datetime`, `current_date`, `current_time`, and `current_timestamp` are defined by the JPQL specification.
116
+ 1. this syntax is rather unpleasant to look at, and
117
+ 2. these literals are assigned types defined by JDBC, instead of date/time types defined in `java.time`.
131
118
132
119
[IMPORTANT]
133
120
====
134
121
The use of date and time types from the `java.sql` package is strongly discouraged!
122
+ Jakarta Persistence 3.2 deprecates support for these types.
135
123
Always use `java.time` types in new code.
136
124
====
137
125
126
+ HQL provides not one, but two alternatives.
127
+
128
+ [cols="~,~,~,~,^15"]
129
+ |===
130
+ | Date/time type | Java type| Braced literal syntax | Explicitly-typed literal syntax | JPA standard
131
+
132
+ | Date | `LocalDate` | `{yyyy-mm-dd}` | `date yyyy-mm-dd` | ✖
133
+ | Time | `LocalTime` | `{hh:mm}` | `time hh:mm` | ✖
134
+ | Time with seconds | `LocalTime` | `{hh:mm:ss}` | `time hh:mm:ss` | ✖
135
+ | Datetime | `LocalDateTime` | `{yyyy-mm-dd hh:mm:ss}` | `datetime yyyy-mm-dd hh:mm:ss` | ✖
136
+ | Datetime with milliseconds | `LocalDateTime` | `{yyyy-mm-dd hh:mm:ss.millis}` | `datetime yyyy-mm-dd hh:mm:ss.millis` | ✖
137
+ | Datetime with an offset | `OffsetDateTime` | `{yyyy-mm-dd hh:mm:ss +hh:mm}` | `datetime yyyy-mm-dd hh:mm:ss +hh:mm` | ✖
138
+ | Datetime with a time zone | `OffsetDateTime` | `{yyyy-mm-dd hh:mm:ss GMT}` | `datetime yyyy-mm-dd hh:mm:ss GMT` | ✖
139
+ |===
140
+
141
+ Literals referring to the current date and time are also available.
142
+
143
+ [cols="~,~,~,^15"]
144
+ |===
145
+ | Date/time type | Java type | Syntax | JPA standard
146
+
147
+ | Date | `java.time.LocalDate` | `local date` | ✔
148
+ | Time | `java.time.LocalTime` | `local time` | ✔
149
+ | Datetime | `java.time.LocalDateTime` | `local datetime` | ✔
150
+ | Offset datetime | `java.time.OffsetDateTime` | `offset datetime` | ✖
151
+ | Instant | `java.time.Instant` | `instant` | ✖
152
+ |===
153
+
154
+ The following are deprecated in JPA 3.2.
155
+ We include them here only for completeness:
156
+
157
+ [cols="~,~,~,~,^15"]
158
+ |===
159
+ | Date/time type | Java type | JPA syntax ✔ | HQL syntax ✖ | JPA standard
160
+
161
+ | JDBC date | `java.sql.Date` 💀| `current_date` | `current date` | 💀
162
+ | JDBC time | `java.sql.Time` 💀| `current_time` | `current time` | 💀
163
+ | JDBC timestamp | `java.sql.Timestamp` 💀| `current_timestamp` | `current timestamp` | 💀
164
+ |===
165
+
138
166
[[duration-literals]]
139
167
==== Duration literals
140
168
0 commit comments