add four code examples of list index/map key to H6 doc

This commit is contained in:
Gavin King 2023-09-04 05:48:33 +02:00
parent a541a64eec
commit 06ec85e829
1 changed files with 33 additions and 1 deletions

View File

@ -633,13 +633,38 @@ The first three options let us map the index of a `List` or key of a `Map` to a
| Annotation | Purpose | JPA-standard | Annotation | Purpose | JPA-standard
| `@OrderColumn` | Specifies the column used to maintain the order of a list | ✔ | `@OrderColumn` | Specifies the column used to maintain the order of a list | ✔
| `@ListIndexBase` | The column value for the first element of the list (zero by default) | ✖
| `@MapKeyColumn` | Specifies the column used to persist the keys of a map | `@MapKeyColumn` | Specifies the column used to persist the keys of a map
(used when the key is of basic type)| ✔ (used when the key is of basic type)| ✔
| `@MapKeyJoinColumn` | Specifies the column used to persist the keys of a map | `@MapKeyJoinColumn` | Specifies the column used to persist the keys of a map
(used when the key is an entity) | ✔ (used when the key is an entity) | ✔
|=== |===
For an unowned `@OneToMany` association, the column must also be mapped on the owning side, usually by an attribute of the target entity. [source,java]
----
@ManyToMany
@OrderColumn // order of list is persistent
List<Author> authors = new ArrayList<>();
----
[source,java]
----
@ElementCollection
@OrderColumn(name="tag_order") @ListIndexBase(1) // order column and base value
List<String> tags;
----
[source,java]
----
@ElementCollection
@CollectionTable(name = "author_bios", // table name
joinColumns = @JoinColumn(name = "book_ssn")) // column holding foreign key of owner
@Column(name="bio") // column holding map values
@MapKeyJoinColumn(name="author_ssn") // column holding map keys
Map<Author,String> biographies;
----
For a `Map` representing an unowned `@OneToMany` association, the column must also be mapped on the owning side, usually by an attribute of the target entity.
In this case we usually use a different annotation: In this case we usually use a different annotation:
.Annotation for mapping an entity attribute to a map key .Annotation for mapping an entity attribute to a map key
@ -650,6 +675,13 @@ In this case we usually use a different annotation:
| `@MapKey` | Specifies an attribute of the target entity which acts as the key of the map | &#10004; | `@MapKey` | Specifies an attribute of the target entity which acts as the key of the map | &#10004;
|=== |===
[source,java]
----
@OneToMany(mappedBy = Book_.PUBLISHER)
@MapKey(name = Book_.TITLE) // the key of the map is the title of the book
Map<String,Book> booksByTitle = new HashMap<>();
----
Now, let's introduce a little distinction: Now, let's introduce a little distinction:
- an _ordered collection_ is one with an ordering maintained in the database, and - an _ordered collection_ is one with an ordering maintained in the database, and