Skip to content

Commit 4d85ee1

Browse files
committed
HHH-16383 - NaturalIdClass
1 parent 75128c2 commit 4d85ee1

File tree

60 files changed

+986
-1175
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+986
-1175
lines changed

hibernate-core/src/main/java/org/hibernate/FindBy.java renamed to hibernate-core/src/main/java/org/hibernate/KeyType.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66

77
import jakarta.persistence.FindOption;
88

9-
/// FindOption allowing to load based on either id (default)
10-
/// or natural-id.
9+
/// FindOption allowing to load based on either id (default) or natural-id.
1110
///
1211
/// @see jakarta.persistence.EntityManager#find
1312
/// @see Session#findMultiple
1413
///
1514
/// @author Steve Ebersole
1615
/// @author Gavin King
17-
public enum FindBy implements FindOption {
16+
public enum KeyType implements FindOption {
1817
/// Indicates to find by the entity's identifier. The default.
1918
///
2019
/// @see jakarta.persistence.Id

hibernate-core/src/main/java/org/hibernate/NaturalIdSynchronization.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import jakarta.persistence.FindOption;
88

99
/// Indicates whether to perform synchronization (a sort of flush)
10-
/// before a [find by natural-id][FindBy#NATURAL_ID].
10+
/// before a [find by natural-id][KeyType#NATURAL_ID].
1111
///
1212
/// @author Steve Ebersole
1313
public enum NaturalIdSynchronization implements FindOption {

hibernate-core/src/main/java/org/hibernate/Session.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ public interface Session extends SharedSessionContract, EntityManager {
476476
///
477477
/// @implNote Note that Hibernate's implementation of this method can
478478
/// also be used for loading an entity by its [natural-id][org.hibernate.annotations.NaturalId]
479-
/// by passing [FindBy#NATURAL_ID] as a [FindOption] and the natural-id value as the `key` to load.
479+
/// by passing [KeyType#NATURAL_ID] as a [FindOption] and the natural-id value as the `key` to load.
480480
@Override
481481
<T> T find(Class<T> entityClass, Object key, FindOption... options);
482482

@@ -1067,7 +1067,7 @@ public interface Session extends SharedSessionContract, EntityManager {
10671067
/// @throws HibernateException If the given class does not resolve as a mapped entity,
10681068
/// or if the entity does not declare a natural id
10691069
///
1070-
/// @deprecated (since 7.3) : Use {@linkplain #find} with [FindBy#NATURAL_ID] instead.
1070+
/// @deprecated (since 7.3) : Use {@linkplain #find} with [KeyType#NATURAL_ID] instead.
10711071
@Deprecated
10721072
<T> NaturalIdLoadAccess<T> byNaturalId(Class<T> entityClass);
10731073

@@ -1083,7 +1083,7 @@ public interface Session extends SharedSessionContract, EntityManager {
10831083
/// @throws HibernateException If the given name does not resolve to a mapped entity,
10841084
/// or if the entity does not declare a natural id
10851085
///
1086-
/// @deprecated (since 7.3) : Use {@linkplain #find} with [FindBy#NATURAL_ID] instead.
1086+
/// @deprecated (since 7.3) : Use {@linkplain #find} with [KeyType#NATURAL_ID] instead.
10871087
@Deprecated
10881088
<T> NaturalIdLoadAccess<T> byNaturalId(String entityName);
10891089

@@ -1099,7 +1099,7 @@ public interface Session extends SharedSessionContract, EntityManager {
10991099
/// @throws HibernateException If the given class does not resolve as a mapped entity,
11001100
/// or if the entity does not declare a natural id
11011101
///
1102-
/// @deprecated (since 7.3) : Use {@linkplain #find} with [FindBy#NATURAL_ID] instead.
1102+
/// @deprecated (since 7.3) : Use {@linkplain #find} with [KeyType#NATURAL_ID] instead.
11031103
@Deprecated
11041104
<T> SimpleNaturalIdLoadAccess<T> bySimpleNaturalId(Class<T> entityClass);
11051105

@@ -1115,7 +1115,7 @@ public interface Session extends SharedSessionContract, EntityManager {
11151115
/// @throws HibernateException If the given name does not resolve to a mapped entity,
11161116
/// or if the entity does not declare a natural id
11171117
///
1118-
/// @deprecated (since 7.3) : Use {@linkplain #find} with [FindBy#NATURAL_ID] instead.
1118+
/// @deprecated (since 7.3) : Use {@linkplain #find} with [KeyType#NATURAL_ID] instead.
11191119
@Deprecated
11201120
<T> SimpleNaturalIdLoadAccess<T> bySimpleNaturalId(String entityName);
11211121

@@ -1130,7 +1130,7 @@ public interface Session extends SharedSessionContract, EntityManager {
11301130
/// @throws HibernateException If the given class does not resolve as a mapped entity,
11311131
/// or if the entity does not declare a natural id
11321132
///
1133-
/// @deprecated (since 7.3) : Use {@linkplain #findMultiple} with [FindBy#NATURAL_ID] instead.
1133+
/// @deprecated (since 7.3) : Use {@linkplain #findMultiple} with [KeyType#NATURAL_ID] instead.
11341134
///
11351135
@Deprecated
11361136
<T> NaturalIdMultiLoadAccess<T> byMultipleNaturalId(Class<T> entityClass);
@@ -1146,7 +1146,7 @@ public interface Session extends SharedSessionContract, EntityManager {
11461146
/// @throws HibernateException If the given name does not resolve to a mapped entity,
11471147
/// or if the entity does not declare a natural id
11481148
///
1149-
/// @deprecated (since 7.3) : Use {@linkplain #findMultiple} with [FindBy#NATURAL_ID] instead.
1149+
/// @deprecated (since 7.3) : Use {@linkplain #findMultiple} with [KeyType#NATURAL_ID] instead.
11501150
@Deprecated
11511151
<T> NaturalIdMultiLoadAccess<T> byMultipleNaturalId(String entityName);
11521152

hibernate-core/src/main/java/org/hibernate/internal/MultiIdentifierLoadAccessImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import jakarta.persistence.Timeout;
1010
import org.hibernate.BatchSize;
1111
import org.hibernate.CacheMode;
12-
import org.hibernate.FindBy;
12+
import org.hibernate.KeyType;
1313
import org.hibernate.LockMode;
1414
import org.hibernate.LockOptions;
1515
import org.hibernate.MultiIdentifierLoadAccess;
@@ -178,7 +178,7 @@ public <K> List<T> multiLoad(K... ids) {
178178
private FindMultipleByKeyOperation<T> buildOperation() {
179179
return new FindMultipleByKeyOperation<T>(
180180
entityPersister,
181-
FindBy.ID,
181+
KeyType.ID,
182182
batchSize,
183183
sessionCheckMode,
184184
removalsMode,

hibernate-core/src/main/java/org/hibernate/internal/NaturalIdMultiLoadAccessStandard.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import jakarta.persistence.Timeout;
1010
import org.hibernate.BatchSize;
1111
import org.hibernate.CacheMode;
12-
import org.hibernate.FindBy;
12+
import org.hibernate.KeyType;
1313
import org.hibernate.LockMode;
1414
import org.hibernate.LockOptions;
1515
import org.hibernate.Locking;
@@ -141,7 +141,7 @@ public List<T> multiLoad(List<?> ids) {
141141
private FindMultipleByKeyOperation<T> buildOperation() {
142142
return new FindMultipleByKeyOperation<T>(
143143
entityDescriptor,
144-
FindBy.NATURAL_ID,
144+
KeyType.NATURAL_ID,
145145
batchSize == null ? null : new BatchSize( batchSize ),
146146
SessionCheckMode.ENABLED,
147147
removalsMode,

hibernate-core/src/main/java/org/hibernate/internal/find/FindByKeyOperation.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import org.checkerframework.checker.nullness.qual.Nullable;
1515
import org.hibernate.CacheMode;
1616
import org.hibernate.EnabledFetchProfile;
17-
import org.hibernate.FindBy;
17+
import org.hibernate.KeyType;
1818
import org.hibernate.FindMultipleOption;
1919
import org.hibernate.LockMode;
2020
import org.hibernate.LockOptions;
@@ -47,16 +47,16 @@
4747
import static org.hibernate.jpa.SpecHints.HINT_SPEC_LOCK_TIMEOUT;
4848
import static org.hibernate.proxy.HibernateProxy.extractLazyInitializer;
4949

50-
/// Support for loading a single entity by key (either [id][FindBy#ID] or [natural-id][FindBy#NATURAL_ID]).
50+
/// Support for loading a single entity by key (either [id][KeyType#ID] or [natural-id][KeyType#NATURAL_ID]).
5151
///
5252
/// @see org.hibernate.Session#find
53-
/// @see FindBy
53+
/// @see KeyType
5454
///
5555
/// @author Steve Ebersole
5656
public class FindByKeyOperation<T> implements NaturalIdLoader.Options {
5757
private final EntityPersister entityDescriptor;
5858

59-
private FindBy findBy = FindBy.ID;
59+
private KeyType keyType = KeyType.ID;
6060

6161
private CacheStoreMode cacheStoreMode;
6262
private CacheRetrieveMode cacheRetrieveMode;
@@ -110,8 +110,8 @@ public FindByKeyOperation(
110110
readOnlyMode = defaultReadOnly ? ReadOnlyMode.READ_ONLY : ReadOnlyMode.READ_WRITE;
111111

112112
for ( FindOption option : findOptions ) {
113-
if ( option instanceof FindBy findBy ) {
114-
this.findBy = findBy;
113+
if ( option instanceof KeyType keyType ) {
114+
this.keyType = keyType;
115115
}
116116
else if ( option instanceof CacheStoreMode cacheStoreMode ) {
117117
this.cacheStoreMode = cacheStoreMode;
@@ -164,7 +164,7 @@ private void enabledFetchProfile(String profileName) {
164164
}
165165

166166
public T performFind(Object key, LoadAccessContext loadAccessContext) {
167-
if ( findBy == FindBy.NATURAL_ID ) {
167+
if ( keyType == KeyType.NATURAL_ID ) {
168168
return findByNaturalId( key, loadAccessContext );
169169
}
170170
else {

hibernate-core/src/main/java/org/hibernate/internal/find/FindMultipleByKeyOperation.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import org.hibernate.BatchSize;
1616
import org.hibernate.CacheMode;
1717
import org.hibernate.EnabledFetchProfile;
18-
import org.hibernate.FindBy;
18+
import org.hibernate.KeyType;
1919
import org.hibernate.LockMode;
2020
import org.hibernate.LockOptions;
2121
import org.hibernate.Locking;
@@ -45,16 +45,16 @@
4545
import static org.hibernate.internal.NaturalIdHelper.performAnyNeededCrossReferenceSynchronizations;
4646
import static org.hibernate.jpa.SpecHints.HINT_SPEC_LOCK_TIMEOUT;
4747

48-
/// Support for loading multiple entities (of a type) by key (either [id][FindBy#ID] or [natural-id][FindBy#NATURAL_ID]).
48+
/// Support for loading multiple entities (of a type) by key (either [id][KeyType#ID] or [natural-id][KeyType#NATURAL_ID]).
4949
///
5050
/// @see org.hibernate.Session#findMultiple
51-
/// @see FindBy
51+
/// @see KeyType
5252
///
5353
/// @author Steve Ebersole
5454
public class FindMultipleByKeyOperation<T> implements MultiIdLoadOptions, MultiNaturalIdLoadOptions {
5555
private final EntityPersister entityDescriptor;
5656

57-
private FindBy findBy = FindBy.ID;
57+
private KeyType keyType = KeyType.ID;
5858

5959
private BatchSize batchSize;
6060
private SessionCheckMode sessionCheckMode = SessionCheckMode.DISABLED;
@@ -107,8 +107,8 @@ public FindMultipleByKeyOperation(
107107
readOnlyMode = defaultReadOnly ? ReadOnlyMode.READ_ONLY : ReadOnlyMode.READ_WRITE;
108108

109109
for ( FindOption option : findOptions ) {
110-
if ( option instanceof FindBy findBy ) {
111-
this.findBy = findBy;
110+
if ( option instanceof KeyType keyType ) {
111+
this.keyType = keyType;
112112
}
113113
else if ( option instanceof BatchSize batchSize ) {
114114
this.batchSize = batchSize;
@@ -176,7 +176,7 @@ public List<T> performFind(
176176
LoadAccessContext loadAccessContext) {
177177
// todo (natural-id-class) : these impls are temporary
178178
// longer term, move the logic here as much of it can be shared
179-
if ( findBy == FindBy.NATURAL_ID ) {
179+
if ( keyType == KeyType.NATURAL_ID ) {
180180
return findByNaturalIds( keys, graphSemantic, rootGraph, loadAccessContext );
181181
}
182182
else {
@@ -310,7 +310,7 @@ public Integer getBatchSize() {
310310
@Deprecated
311311
public FindMultipleByKeyOperation(
312312
EntityPersister entityDescriptor,
313-
FindBy findBy,
313+
KeyType keyType,
314314
BatchSize batchSize,
315315
SessionCheckMode sessionCheckMode,
316316
RemovalsMode removalsMode,
@@ -328,7 +328,7 @@ public FindMultipleByKeyOperation(
328328
lockOptions = LockOptions.NONE;
329329
}
330330
this.entityDescriptor = entityDescriptor;
331-
this.findBy = findBy;
331+
this.keyType = keyType;
332332
this.batchSize = batchSize;
333333
this.sessionCheckMode = sessionCheckMode;
334334
this.removalsMode = removalsMode;

hibernate-core/src/main/java/org/hibernate/loader/ast/internal/AbstractNaturalIdLoader.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,14 @@ protected <R> R executeNaturalIdQuery(
257257
Consumer<Predicate> predicateConsumer,
258258
LoaderSqlAstCreationState sqlAstCreationState,
259259
SharedSessionContractImplementor session) {
260+
assert naturalIdMapping.isNormalized( naturalIdValue );
261+
260262
final var factory = session.getFactory();
261263

262264
final var bindings =
263265
new JdbcParameterBindingsImpl( naturalIdMapping.getJdbcTypeCount() );
264266
applyNaturalIdRestriction(
265-
naturalIdMapping().normalizeInput( naturalIdValue ),
267+
naturalIdValue,
266268
rootTableGroup,
267269
predicateConsumer,
268270
bindings::addBinding,

hibernate-core/src/main/java/org/hibernate/loader/ast/spi/MultiIdEntityLoader.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
*/
55
package org.hibernate.loader.ast.spi;
66

7-
import java.util.List;
8-
97
import org.hibernate.engine.spi.SharedSessionContractImplementor;
108

11-
/**
12-
* Loader subtype for loading multiple entities by multiple identifier values.
13-
*/
9+
import java.util.List;
10+
11+
/// EntityMultiLoader implementation based on [identifier][org.hibernate.KeyType#ID].
12+
///
13+
/// @see org.hibernate.Session#findMultiple
14+
///
15+
/// @author Steve Ebersole
1416
public interface MultiIdEntityLoader<T> extends EntityMultiLoader<T> {
1517
/**
1618
* Load multiple entities by id. The exact result depends on the passed options.

hibernate-core/src/main/java/org/hibernate/loader/ast/spi/MultiIdLoadOptions.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@
77
import org.hibernate.SessionCheckMode;
88
import org.hibernate.engine.spi.SessionImplementor;
99

10-
/**
11-
* Encapsulation of the options for loading multiple entities by id
12-
*/
10+
11+
/// Encapsulation of the options for loading multiple entities (of a type)
12+
/// by [id][org.hibernate.KeyType#ID].
13+
///
14+
/// @see org.hibernate.Session#findMultiple
15+
/// @see MultiIdEntityLoader
16+
///
17+
/// @author Steve Ebersole
1318
public interface MultiIdLoadOptions extends MultiLoadOptions {
1419
/**
1520
* Controls whether to check the current status of each identified entity

0 commit comments

Comments
 (0)