Skip to content

Commit 6480154

Browse files
committed
more updates for JPA 3.2
Signed-off-by: Gavin King <[email protected]>
1 parent 82b20a0 commit 6480154

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed

documentation/src/main/asciidoc/introduction/Entities.adoc

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ This id maps to a SQL `identity`, `auto_increment`, or `bigserial` column:
250250

251251
[source,java]
252252
----
253-
@Id @GeneratedValue(strategy=IDENTITY) Long id;
253+
@Id @GeneratedValue(strategy = IDENTITY) Long id;
254254
----
255255

256256
The `@SequenceGenerator` and `@TableGenerator` annotations allow further control over `SEQUENCE` and `TABLE` generation respectively.
@@ -259,7 +259,7 @@ Consider this sequence generator:
259259

260260
[source,java]
261261
----
262-
@SequenceGenerator(name="bookSeq", sequenceName="seq_book", initialValue = 5, allocationSize=10)
262+
@SequenceGenerator(name = "bookSeq", sequenceName = "seq_book", initialValue = 5, allocationSize=10)
263263
----
264264

265265
Values are generated using a database sequence defined as follows:
@@ -285,7 +285,7 @@ Any identifier attribute may now make use of the generator named `bookSeq`:
285285
[source,java]
286286
----
287287
@Id
288-
@GeneratedValue(strategy=SEQUENCE, generator="bookSeq") // reference to generator defined elsewhere
288+
@GeneratedValue(generator = "bookSeq") // reference to generator defined elsewhere
289289
Long id;
290290
----
291291

@@ -294,18 +294,38 @@ Actually, it's extremely common to place the `@SequenceGenerator` annotation on
294294
[source,java]
295295
----
296296
@Id
297-
@GeneratedValue(strategy=SEQUENCE, generator="bookSeq") // reference to generator defined below
298-
@SequenceGenerator(name="bookSeq", sequenceName="seq_book", initialValue = 5, allocationSize=10)
297+
@GeneratedValue // uses the generator defined below
298+
@SequenceGenerator(sequenceName = "seq_book", initialValue = 5, allocationSize=10)
299299
Long id;
300300
----
301301

302-
[NOTE]
303-
// .JPA id generators may be shared between entities
304-
====
305-
JPA id generators may be shared between entities.
306-
A `@SequenceGenerator` or `@TableGenerator` must have a name, and may be shared between multiple id attributes.
307-
This fits somewhat uncomfortably with the common practice of annotating the `@Id` attribute which makes use of the generator!
308-
====
302+
In this case, the `name` of the `@SequenceGenerator` should not be specified.
303+
304+
We may even place a `@SequenceGenerator` or `@TableGenerator` annotation at the package level:
305+
306+
[source,java]
307+
----
308+
@SequenceGenerator(sequenceName = "id_sequence", initialValue = 5, allocationSize=10)
309+
@TableGenerator(table = "id_table", initialValue = 5, allocationSize=10)
310+
package org.example.entities;
311+
----
312+
313+
Then any entity in this package which specifies `strategy=SEQUENCE` or `strategy=TABLE` without also explicitly specifying a generator `name` will be assigned a generator based on the package-level annotation.
314+
315+
[source,java]
316+
----
317+
@Id
318+
@GeneratedValue(strategy=SEQUENCE) // uses the sequence generator defined at the package level
319+
Long id;
320+
----
321+
322+
// [NOTE]
323+
// // .JPA id generators may be shared between entities
324+
// ====
325+
// JPA id generators may be shared between entities.
326+
// A `@SequenceGenerator` or `@TableGenerator` must have a name, and may be shared between multiple id attributes.
327+
// This fits somewhat uncomfortably with the common practice of annotating the `@Id` attribute which makes use of the generator!
328+
// ====
309329

310330
As you can see, JPA provides quite adequate support for the most common strategies for system-generated ids.
311331
However, the annotations themselves are a bit more intrusive than they should be, and there's no well-defined way to extend this framework to support custom strategies for id generation.

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,10 @@ The operator precedence is given by this table, from highest to lowest precedenc
316316
[[concatenation]]
317317
==== String concatenation
318318

319-
HQL defines two ways to concatenate strings:
319+
JPQL defines two ways to concatenate strings:
320320

321321
* the SQL-style concatenation operator, `||`, and
322-
* the JPQL-standard `concat()` function.
322+
* the standard `concat()` function.
323323

324324
See <<string-functions, below>> for details of the `concat()` function.
325325

@@ -545,7 +545,7 @@ The following special functions make it possible to discover or narrow expressio
545545

546546
| `type()` | The (concrete) entity or embeddable type | `type(e)` | ✔
547547
| `treat()` | Narrow an entity or embeddable type | `treat(e as Entity)` | ✔
548-
| `cast()` | Narrow a basic type | `cast(x as Type)` |
548+
| `cast()` | Narrow a basic type | `cast(x as Type)` |
549549
| `str()` | Cast to a string | `str(x)` | ✖
550550
| `ordinal()` | Get the ordinal value of an enum | `ordinal(x)` | ✖
551551
|===
@@ -836,9 +836,9 @@ Naturally, there are a good number of functions for working with strings.
836836
`pad(str with len leading)`, +
837837
`pad(str with len trailing)`, or +
838838
`pad(str with len leading char)` | ✖ / ✖
839-
| `left()` | The leftmost characters of a string | `left(str, len)` | / ✖
840-
| `right()` | The rightmost characters of a string | `right(str, len)` | / ✖
841-
| `replace()` | Replace every occurrence of a pattern in a string | `replace(str, patt, rep)` | / ✖
839+
| `left()` | The leftmost characters of a string | `left(str, len)` | / ✖
840+
| `right()` | The rightmost characters of a string | `right(str, len)` | / ✖
841+
| `replace()` | Replace every occurrence of a pattern in a string | `replace(str, patt, rep)` | / ✖
842842
| `repeat()` | Concatenate a string with itself multiple times | `repeat(str, times)` | ✖ / ✖
843843
| `collate()` | Select a collation | `collate(p.name as collation)` | ✖ / ✖
844844
|===

0 commit comments

Comments
 (0)