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/Entities.adoc
+14Lines changed: 14 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -526,6 +526,20 @@ Hibernate automatically generates a `UNIQUE` constraint on the columns mapped by
526
526
Consider using the natural id attributes to implement <<equals-and-hash>>.
527
527
====
528
528
529
+
In cases where the natural id is defined by multiple attributes, Hibernate also offers the link:{doc-javadoc-url}org/hibernate/annotations/NaturalIdClass.html[`@NaturalIdClass`] annotation which acts similarly to the Jakarta Persistence `@IdClass` annotation for <<load-by-natural-id,find operations>> -
530
+
531
+
[source,java]
532
+
----
533
+
record BookKey(String isbn, int printing) {}
534
+
535
+
@Entity
536
+
@NaturalIdClass(BookKey.class)
537
+
class Book {
538
+
...
539
+
}
540
+
----
541
+
542
+
529
543
The payoff for doing this extra work, as we will see <<natural-id-cache,much later>>, is that we can take advantage of optimized natural id lookups that make use of the second-level cache.
530
544
531
545
Note that even when you've identified a natural key, we still recommend the use of a generated surrogate key in foreign keys, since this makes your data model _much_ easier to change.
As discussed <<natural-id-attributes,earlier>>, Hibernate offers the ability to map a natural id and perform load operations using that natural id.
374
+
This is accomplished using the `KeyType#NATURAL` `FindOption` -
375
+
376
+
[source,java]
377
+
----
378
+
var bookKey = new BookKey(...);
379
+
var book = session.find(Book.class, bookKey, NATURAL);
380
+
var books = session.findMultiple(Book.class, List.of(bookKey), NATURAL);
381
+
----
382
+
383
+
When loading by natural id, the type of value accepted depends on the type of natural id.
384
+
For single-attribute natural ids, whether defined by a basic or embedded type, the attribute type should be used.
385
+
For multi-attribute natural ids, Hibernate will accept a number of forms:
386
+
387
+
* If a link:{doc-javadoc-url}org/hibernate/annotations/NaturalIdClass.html[`@NaturalIdClass`] is defined, an instance of the natural id class may be used.
388
+
* A `List` of the individual attribute values, ordered alphabetically by name, may be used.
389
+
* A `Map` of the individual attribute values, keyed by the attribute name, may be used.
390
+
371
391
Each of the operations we've seen so far affects a single entity instance passed as an argument.
372
392
But there's a way to set things up so that an operation will propagate to associated entities.
373
393
@@ -595,7 +615,8 @@ Therefore, Hibernate has some APIs that streamline certain more complicated look
595
615
596
616
[NOTE]
597
617
====
598
-
Since the introduction of `FindOption` in JPA 3.2, `byId()` is now much less useful.
618
+
Since the introduction of `FindOption` in JPA 3.2, `byId()` is now much less useful and deprecated.
619
+
Instead, use `find()` and `findMultiple()` as discussed <<methods-for-reading,earlier>>.
599
620
====
600
621
601
622
Batch loading is very useful when we need to retrieve multiple instances of the same entity class by id:
0 commit comments