Skip to content

Commit 08cef4d

Browse files
committed
add more code examples to @naturalid
cc @sebersole
1 parent 65ad36a commit 08cef4d

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

hibernate-core/src/main/java/org/hibernate/annotations/NaturalId.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/// the primary key of an entity class is a surrogate key, that is,
1818
/// a {@linkplain jakarta.persistence.GeneratedValue system-generated}
1919
/// synthetic identifier, with no domain-model semantics. There should
20-
/// always be some other field or combination of fields which uniquely
20+
/// always be some other field or combination of fields that uniquely
2121
/// identifies an instance of the entity from the point of view of the
2222
/// user of the system. This is the _natural id_ of the entity.
2323
///
@@ -76,21 +76,41 @@
7676
/// The [org.hibernate.Session] interface offers several methods
7777
/// that allow retrieval of one or more entity references by natural-id
7878
/// allow an entity instance to be retrieved by its natural-id:
79-
/// * [org.hibernate.Session#findByNaturalId] allows loading a single
80-
/// entity instance by natural-id.
81-
/// * [org.hibernate.Session#findMultipleByNaturalId] allows loading multiple
82-
/// entity instances by natural-id.
79+
/// * [org.hibernate.Session#find(Class, Object, jakarta.persistence.FindOption...)]
80+
/// allows loading a single entity instance by natural-id.
81+
/// * [org.hibernate.Session#findMultiple(Class, java.util.List, jakarta.persistence.FindOption...)]
82+
/// allows loading multiple entity instances by natural-id.
83+
///
84+
/// ```
85+
/// Person person = session.find(Person.class, ssn, KeyType.NATURAL);
86+
/// ```
87+
/// ```
88+
/// Vehicle vehicle =
89+
/// session.find(Vehicle.class,
90+
/// Map.of(Vehicle_.REGION, region,
91+
/// Vehicle_.REGISTRATION, registration),
92+
/// KeyType.NATURAL);
93+
/// ```
94+
/// ```
95+
/// List<Person> people = session.findMultiple(Person.class, ssns, KeyType.NATURAL);
96+
/// ```
8397
///
8498
/// If the entity is also marked for [natural id caching][NaturalIdCache],
8599
/// then these methods may be able to avoid a database round trip.
86100
///
87-
/// @see org.hibernate.Session#findByNaturalId
88-
/// @see org.hibernate.Session#findMultipleByNaturalId
101+
/// @see org.hibernate.Session#find(Class, Object, jakarta.persistence.FindOption...)
102+
/// @see org.hibernate.Session#findMultiple(Class, java.util.List, jakarta.persistence.FindOption...)
89103
/// @see NaturalIdClass
90104
/// @see NaturalIdCache
91105
///
92106
/// @apiNote For non-aggregated composite natural-id cases, it is recommended to
93107
/// leverage [@NaturalIdClass][NaturalIdClass] for loading.
108+
/// ```
109+
/// Vehicle vehicle =
110+
/// session.find(Vehicle.class,
111+
/// VehicleKey(region, registration),
112+
/// KeyType.NATURAL);
113+
/// ```
94114
///
95115
/// @author Nicolás Lichtmaier
96116
@Target({METHOD, FIELD})

0 commit comments

Comments
 (0)