Skip to content

Commit a624b2d

Browse files
committed
update documentation of date/time literals for JPA 3.2
and fix something which was actually wrong: the JDBC-style literals are assigned types from java.sql, not java.time.
1 parent aca7e9e commit a624b2d

File tree

1 file changed

+55
-27
lines changed

1 file changed

+55
-27
lines changed

documentation/src/main/asciidoc/querylanguage/Expressions.adoc

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -96,45 +96,73 @@ Similarly, the Java-style postfix is case-insensitive.
9696
[[datetime-literals]]
9797
==== Date and time literals
9898

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

101+
[cols="~,~,~,^15"]
102102
|===
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'}` |
112112
|===
113113

114-
Literals referring to the current date and time are also provided.
115-
Again there is some flexibility.
114+
There's two problems here:
116115

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

132119
[IMPORTANT]
133120
====
134121
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.
135123
Always use `java.time` types in new code.
136124
====
137125

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+
138166
[[duration-literals]]
139167
==== Duration literals
140168

0 commit comments

Comments
 (0)