Skip to content

Commit f348207

Browse files
committed
clarifications in section on collection mapping
(to agree with blog)
1 parent 2a9f4c4 commit f348207

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,12 @@ But if you feel like you _really_ need a collection with a fancier structure tha
713713

714714
TIP: For more detail about the use of these annotations, please refer to https://in.relation.to/2024/11/12/-what-collection/[this post on the Hibernate blog].
715715

716-
The first three options let us map the index of a `List` or key of a `Map` to a column, and are usually used with `@ElementCollection`, or on the owning side of an association:
716+
The following options let us map the index of a `List` or key of a `Map` to a column, and are used with:
717+
718+
- `@ElementCollection`, or
719+
- on the owning side of an association.
720+
721+
They should not be used on the unowned (that is, `mappedBy`) side of an association.
717722

718723
.Annotations for mapping lists and maps
719724
[%breakable,cols="22,~,^13"]
@@ -728,20 +733,27 @@ The first three options let us map the index of a `List` or key of a `Map` to a
728733
(used when the key is an entity) | ✔
729734
|===
730735

736+
The name of the `@OrderColumn` or `@MapKeyColumn` may be defaulted, for example:
737+
731738
[source,java]
732739
----
733740
@ManyToMany
734741
@OrderColumn // order of list is persistent
735742
List<Author> authors = new ArrayList<>();
736743
----
737744

745+
But it's usually better to specify the column name explicitly:
746+
738747
[source,java]
739748
----
740749
@ElementCollection
741-
@OrderColumn(name="tag_order") @ListIndexBase(1) // order column and base value
750+
@OrderColumn(name="tag_order")
751+
@ListIndexBase(1) // order column and base value
742752
List<String> tags;
743753
----
744754

755+
Such mappings can get pretty complicated:
756+
745757
[source,java]
746758
----
747759
@ElementCollection
@@ -752,8 +764,10 @@ List<String> tags;
752764
Map<Author,String> biographies;
753765
----
754766

755-
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.
756-
In this case we usually use a different annotation:
767+
As you can imagine, we think you should use such mappings very sparingly, if at all.
768+
769+
For a `Map` representing an unowned `@OneToMany` association, the column holding the key of the map must also be mapped on the owning side, usually by an attribute of the target entity.
770+
In this case we use a different annotation:
757771

758772
.Annotation for mapping an entity attribute to a map key
759773
[%breakable,cols="22,~,^13"]
@@ -763,13 +777,17 @@ In this case we usually use a different annotation:
763777
| `@MapKey` | Specifies an attribute of the target entity which acts as the key of the map | &#10004;
764778
|===
765779

780+
Note that `@MapKey` specifies a field or property name, not a column name.
781+
766782
[source,java]
767783
----
768784
@OneToMany(mappedBy = Book_.PUBLISHER)
769785
@MapKey(name = Book_.TITLE) // the key of the map is the title of the book
770786
Map<String,Book> booksByTitle = new HashMap<>();
771787
----
772788

789+
In fact, `@MapKey` may also be used for owned collections.
790+
773791
Now, let's introduce a little distinction:
774792

775793
- an _ordered collection_ is one with an ordering maintained in the database, and

0 commit comments

Comments
 (0)