Skip to content

Commit 75128c2

Browse files
committed
HHH-16383 - NaturalIdClass
1 parent 159be18 commit 75128c2

File tree

5 files changed

+105
-263
lines changed

5 files changed

+105
-263
lines changed

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

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,39 @@
77
import jakarta.persistence.EntityGraph;
88
import jakarta.persistence.PessimisticLockScope;
99
import jakarta.persistence.Timeout;
10+
import org.hibernate.BatchSize;
1011
import org.hibernate.CacheMode;
12+
import org.hibernate.FindBy;
1113
import org.hibernate.LockMode;
1214
import org.hibernate.LockOptions;
1315
import org.hibernate.MultiIdentifierLoadAccess;
16+
import org.hibernate.NaturalIdSynchronization;
1417
import org.hibernate.OrderingMode;
18+
import org.hibernate.ReadOnlyMode;
1519
import org.hibernate.RemovalsMode;
1620
import org.hibernate.SessionCheckMode;
1721
import org.hibernate.UnknownProfileException;
1822
import org.hibernate.engine.spi.SessionImplementor;
1923
import org.hibernate.engine.spi.SharedSessionContractImplementor;
2024
import org.hibernate.graph.GraphSemantic;
2125
import org.hibernate.graph.spi.RootGraphImplementor;
26+
import org.hibernate.internal.find.FindMultipleByKeyOperation;
2227
import org.hibernate.loader.ast.spi.MultiIdLoadOptions;
28+
import org.hibernate.loader.internal.LoadAccessContext;
2329
import org.hibernate.persister.entity.EntityPersister;
2430

2531
import java.util.HashSet;
2632
import java.util.List;
2733
import java.util.Set;
28-
import java.util.function.Supplier;
2934

3035
import static java.util.Collections.emptyList;
3136

32-
/**
33-
* @author Steve Ebersole
34-
*/
37+
/// Implementation of MultiIdentifierLoadAccess.
38+
///
39+
/// @author Steve Ebersole
40+
///
41+
/// @deprecated Use [FindMultipleByKeyOperation] instead.
42+
@Deprecated
3543
class MultiIdentifierLoadAccessImpl<T> implements MultiIdentifierLoadAccess<T>, MultiIdLoadOptions {
3644
private final SharedSessionContractImplementor session;
3745
private final EntityPersister entityPersister;
@@ -43,7 +51,7 @@ class MultiIdentifierLoadAccessImpl<T> implements MultiIdentifierLoadAccess<T>,
4351
private RootGraphImplementor<T> rootGraph;
4452
private GraphSemantic graphSemantic;
4553

46-
private Integer batchSize;
54+
private BatchSize batchSize;
4755
private SessionCheckMode sessionCheckMode = SessionCheckMode.DISABLED;
4856
private RemovalsMode removalsMode = RemovalsMode.REPLACE;
4957
protected OrderingMode orderingMode = OrderingMode.ORDERED;
@@ -107,12 +115,12 @@ public MultiIdentifierLoadAccess<T> with(EntityGraph<T> graph, GraphSemantic sem
107115

108116
@Override
109117
public Integer getBatchSize() {
110-
return batchSize;
118+
return batchSize.batchSize();
111119
}
112120

113121
@Override
114122
public MultiIdentifierLoadAccess<T> withBatchSize(int batchSize) {
115-
this.batchSize = batchSize < 1 ? null : batchSize;
123+
this.batchSize = batchSize < 1 ? null : new BatchSize( batchSize );
116124
return this;
117125
}
118126

@@ -164,53 +172,33 @@ public Boolean getReadOnly(SessionImplementor session) {
164172
@Override
165173
@SuppressWarnings( "unchecked" )
166174
public <K> List<T> multiLoad(K... ids) {
167-
return perform( () -> (List<T>) entityPersister.multiLoad( ids, session, this ) );
168-
}
169-
170-
public List<T> perform(Supplier<List<T>> executor) {
171-
final var sessionCacheMode = session.getCacheMode();
172-
boolean cacheModeChanged = false;
173-
if ( cacheMode != null ) {
174-
// naive check for now...
175-
// todo : account for "conceptually equal"
176-
if ( cacheMode != sessionCacheMode ) {
177-
session.setCacheMode( cacheMode );
178-
cacheModeChanged = true;
179-
}
180-
}
175+
return buildOperation().performFind( List.of( ids ), graphSemantic, rootGraph, (LoadAccessContext) session );
176+
}
181177

182-
try {
183-
final var influencers = session.getLoadQueryInfluencers();
184-
final var fetchProfiles =
185-
influencers.adjustFetchProfiles( disabledFetchProfiles, enabledFetchProfiles );
186-
final var effectiveEntityGraph =
187-
rootGraph == null
188-
? null
189-
: influencers.applyEntityGraph( rootGraph, graphSemantic );
190-
try {
191-
return executor.get();
192-
}
193-
finally {
194-
if ( effectiveEntityGraph != null ) {
195-
effectiveEntityGraph.clear();
196-
}
197-
influencers.setEnabledFetchProfileNames( fetchProfiles );
198-
}
199-
}
200-
finally {
201-
if ( cacheModeChanged ) {
202-
// change it back
203-
session.setCacheMode( sessionCacheMode );
204-
}
205-
}
178+
private FindMultipleByKeyOperation<T> buildOperation() {
179+
return new FindMultipleByKeyOperation<T>(
180+
entityPersister,
181+
FindBy.ID,
182+
batchSize,
183+
sessionCheckMode,
184+
removalsMode,
185+
orderingMode,
186+
cacheMode,
187+
lockOptions,
188+
readOnly == Boolean.TRUE ? ReadOnlyMode.READ_ONLY : ReadOnlyMode.READ_WRITE,
189+
enabledFetchProfiles,
190+
disabledFetchProfiles,
191+
// irrelevant for load-by-id
192+
NaturalIdSynchronization.DISABLED
193+
);
206194
}
207195

208196
@Override
209197
@SuppressWarnings( "unchecked" )
210198
public <K> List<T> multiLoad(List<K> ids) {
211199
return ids.isEmpty()
212200
? emptyList()
213-
: perform( () -> (List<T>) entityPersister.multiLoad( ids.toArray(), session, this ) );
201+
: buildOperation().performFind( (List<Object>)ids, graphSemantic, rootGraph, (LoadAccessContext) session );
214202
}
215203

216204
@Override

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

Lines changed: 35 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,37 @@
44
*/
55
package org.hibernate.internal;
66

7-
import java.util.List;
8-
97
import jakarta.persistence.EntityGraph;
10-
118
import jakarta.persistence.PessimisticLockScope;
129
import jakarta.persistence.Timeout;
10+
import org.hibernate.BatchSize;
1311
import org.hibernate.CacheMode;
12+
import org.hibernate.FindBy;
1413
import org.hibernate.LockMode;
1514
import org.hibernate.LockOptions;
1615
import org.hibernate.Locking;
1716
import org.hibernate.NaturalIdMultiLoadAccess;
17+
import org.hibernate.NaturalIdSynchronization;
1818
import org.hibernate.OrderingMode;
19+
import org.hibernate.ReadOnlyMode;
1920
import org.hibernate.RemovalsMode;
21+
import org.hibernate.SessionCheckMode;
2022
import org.hibernate.engine.spi.SharedSessionContractImplementor;
2123
import org.hibernate.graph.GraphSemantic;
2224
import org.hibernate.graph.spi.RootGraphImplementor;
25+
import org.hibernate.internal.find.FindMultipleByKeyOperation;
2326
import org.hibernate.loader.ast.spi.MultiNaturalIdLoadOptions;
27+
import org.hibernate.loader.internal.LoadAccessContext;
2428
import org.hibernate.persister.entity.EntityPersister;
2529

26-
import static org.hibernate.internal.NaturalIdHelper.performAnyNeededCrossReferenceSynchronizations;
30+
import java.util.List;
2731

28-
/**
29-
* @author Steve Ebersole
30-
*/
32+
/// Implementation of NaturalIdMultiLoadAccess.
33+
///
34+
/// @deprecated Use [FindMultipleByKeyOperation] instead.
35+
///
36+
/// @author Steve Ebersole
37+
@Deprecated
3138
public class NaturalIdMultiLoadAccessStandard<T> implements NaturalIdMultiLoadAccess<T>, MultiNaturalIdLoadOptions {
3239
private final EntityPersister entityDescriptor;
3340
private final SharedSessionContractImplementor session;
@@ -121,68 +128,31 @@ public void with(OrderingMode orderingMode) {
121128
@Override
122129
@SuppressWarnings( "unchecked" )
123130
public List<T> multiLoad(Object... ids) {
124-
performAnyNeededCrossReferenceSynchronizations( true, entityDescriptor, session );
125-
126-
final CacheMode sessionCacheMode = session.getCacheMode();
127-
boolean cacheModeChanged = false;
128-
129-
if ( cacheMode != null ) {
130-
// naive check for now...
131-
// todo : account for "conceptually equal"
132-
if ( cacheMode != sessionCacheMode ) {
133-
session.setCacheMode( cacheMode );
134-
cacheModeChanged = true;
135-
}
136-
}
137-
138-
final var loadQueryInfluencers = session.getLoadQueryInfluencers();
139-
140-
try {
141-
final var effectiveEntityGraph = loadQueryInfluencers.getEffectiveEntityGraph();
142-
final var initialGraphSemantic = effectiveEntityGraph.getSemantic();
143-
final var initialGraph = effectiveEntityGraph.getGraph();
144-
final boolean hadInitialGraph = initialGraphSemantic != null;
145-
146-
if ( graphSemantic != null ) {
147-
if ( rootGraph == null ) {
148-
throw new IllegalArgumentException( "Graph semantic specified, but no RootGraph was supplied" );
149-
}
150-
effectiveEntityGraph.applyGraph( rootGraph, graphSemantic );
151-
}
152-
153-
try {
154-
// normalize the incoming natural-id values
155-
for ( int i = 0; i < ids.length; i++ ) {
156-
ids[i] = entityDescriptor.getNaturalIdMapping().normalizeInput( ids[ i ] );
157-
}
158-
159-
return (List<T>)
160-
entityDescriptor.getMultiNaturalIdLoader()
161-
.multiLoad( ids, this, session );
162-
}
163-
finally {
164-
if ( graphSemantic != null ) {
165-
if ( hadInitialGraph ) {
166-
effectiveEntityGraph.applyGraph( initialGraph, initialGraphSemantic );
167-
}
168-
else {
169-
effectiveEntityGraph.clear();
170-
}
171-
}
172-
}
173-
}
174-
finally {
175-
if ( cacheModeChanged ) {
176-
// change it back
177-
session.setCacheMode( sessionCacheMode );
178-
}
179-
}
180-
131+
return buildOperation()
132+
.performFind( List.of( ids ), graphSemantic, rootGraph, (LoadAccessContext) session );
181133
}
182134

183135
@Override
184136
public List<T> multiLoad(List<?> ids) {
185-
return multiLoad( ids.toArray( new Object[ 0 ] ) );
137+
return buildOperation()
138+
.performFind( (List<Object>) ids, graphSemantic, rootGraph, (LoadAccessContext) session );
139+
}
140+
141+
private FindMultipleByKeyOperation<T> buildOperation() {
142+
return new FindMultipleByKeyOperation<T>(
143+
entityDescriptor,
144+
FindBy.NATURAL_ID,
145+
batchSize == null ? null : new BatchSize( batchSize ),
146+
SessionCheckMode.ENABLED,
147+
removalsMode,
148+
orderingMode,
149+
cacheMode,
150+
lockOptions,
151+
session.isDefaultReadOnly() ? ReadOnlyMode.READ_ONLY : ReadOnlyMode.READ_WRITE,
152+
null,
153+
null,
154+
NaturalIdSynchronization.ENABLED
155+
);
186156
}
187157

188158
@Override

0 commit comments

Comments
 (0)