You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documentation/src/main/asciidoc/introduction/Advanced.adoc
+22-4Lines changed: 22 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -713,7 +713,12 @@ But if you feel like you _really_ need a collection with a fancier structure tha
713
713
714
714
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].
715
715
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.
717
722
718
723
.Annotations for mapping lists and maps
719
724
[%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
728
733
(used when the key is an entity) | ✔
729
734
|===
730
735
736
+
The name of the `@OrderColumn` or `@MapKeyColumn` may be defaulted, for example:
737
+
731
738
[source,java]
732
739
----
733
740
@ManyToMany
734
741
@OrderColumn // order of list is persistent
735
742
List<Author> authors = new ArrayList<>();
736
743
----
737
744
745
+
But it's usually better to specify the column name explicitly:
746
+
738
747
[source,java]
739
748
----
740
749
@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
742
752
List<String> tags;
743
753
----
744
754
755
+
Such mappings can get pretty complicated:
756
+
745
757
[source,java]
746
758
----
747
759
@ElementCollection
@@ -752,8 +764,10 @@ List<String> tags;
752
764
Map<Author,String> biographies;
753
765
----
754
766
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:
757
771
758
772
.Annotation for mapping an entity attribute to a map key
759
773
[%breakable,cols="22,~,^13"]
@@ -763,13 +777,17 @@ In this case we usually use a different annotation:
763
777
| `@MapKey` | Specifies an attribute of the target entity which acts as the key of the map | ✔
764
778
|===
765
779
780
+
Note that `@MapKey` specifies a field or property name, not a column name.
781
+
766
782
[source,java]
767
783
----
768
784
@OneToMany(mappedBy = Book_.PUBLISHER)
769
785
@MapKey(name = Book_.TITLE) // the key of the map is the title of the book
770
786
Map<String,Book> booksByTitle = new HashMap<>();
771
787
----
772
788
789
+
In fact, `@MapKey` may also be used for owned collections.
790
+
773
791
Now, let's introduce a little distinction:
774
792
775
793
- an _ordered collection_ is one with an ordering maintained in the database, and
0 commit comments