Skip to content

Commit 6eacabb

Browse files
committed
move management of fetch profiles to the SqlTranslationEngine
1 parent 3491101 commit 6eacabb

File tree

7 files changed

+73
-34
lines changed

7 files changed

+73
-34
lines changed

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,21 @@
3333
*/
3434
public class FetchProfileHelper {
3535

36+
@SuppressWarnings("unused")
3637
public static Map<String, FetchProfile> getFetchProfiles(
3738
MetadataImplementor bootMetamodel,
3839
RuntimeMetamodels runtimeMetamodels) {
3940
final Map<String, FetchProfile> fetchProfiles = new HashMap<>();
41+
addFetchProfiles( bootMetamodel, runtimeMetamodels, fetchProfiles );
42+
return fetchProfiles;
43+
}
44+
45+
static void addFetchProfiles(MetadataImplementor bootMetamodel, RuntimeMetamodels runtimeMetamodels, Map<String, FetchProfile> fetchProfiles) {
4046
for ( org.hibernate.mapping.FetchProfile mappingProfile : bootMetamodel.getFetchProfiles() ) {
4147
final FetchProfile fetchProfile = createFetchProfile( runtimeMetamodels.getMappingMetamodel(), mappingProfile );
4248
fetchProfiles.put( fetchProfile.getName(), fetchProfile );
4349
}
4450
fetchProfiles.put( HIBERNATE_DEFAULT_PROFILE, new DefaultFetchProfile( runtimeMetamodels ) );
45-
return fetchProfiles;
4651
}
4752

4853
private static FetchProfile createFetchProfile(
@@ -53,7 +58,9 @@ private static FetchProfile createFetchProfile(
5358
for ( org.hibernate.mapping.FetchProfile.Fetch mappingFetch : mappingProfile.getFetches() ) {
5459
// resolve the persister owning the fetch
5560
final EntityPersister owner = getEntityPersister( mappingMetamodel, fetchProfile, mappingFetch );
56-
( (FetchProfileAffectee) owner ).registerAffectingFetchProfile( profileName);
61+
if ( owner instanceof FetchProfileAffectee fetchProfileAffectee ) {
62+
fetchProfileAffectee.registerAffectingFetchProfile( profileName );
63+
}
5764

5865
final Association association = new Association( owner, mappingFetch.getAssociation() );
5966
final FetchStyle fetchStyle = fetchStyle( mappingFetch.getMethod() );
@@ -62,8 +69,8 @@ private static FetchProfile createFetchProfile(
6269
// validate the specified association fetch
6370
final ModelPart fetchablePart = owner.findByPath( association.getAssociationPath() );
6471
validateFetchablePart( fetchablePart, profileName, association );
65-
if ( fetchablePart instanceof FetchProfileAffectee ) {
66-
( (FetchProfileAffectee) fetchablePart ).registerAffectingFetchProfile( profileName );
72+
if ( fetchablePart instanceof FetchProfileAffectee fetchProfileAffectee ) {
73+
fetchProfileAffectee.registerAffectingFetchProfile( profileName );
6774
}
6875

6976
// then register the association with the FetchProfile
@@ -73,16 +80,11 @@ private static FetchProfile createFetchProfile(
7380
}
7481

7582
private static FetchStyle fetchStyle(FetchMode fetchMode) {
76-
switch ( fetchMode ) {
77-
case JOIN:
78-
return FetchStyle.JOIN;
79-
case SELECT:
80-
return FetchStyle.SELECT;
81-
case SUBSELECT:
82-
return FetchStyle.SUBSELECT;
83-
default:
84-
throw new IllegalArgumentException( "Unknown FetchMode" );
85-
}
83+
return switch ( fetchMode ) {
84+
case JOIN -> FetchStyle.JOIN;
85+
case SELECT -> FetchStyle.SELECT;
86+
case SUBSELECT -> FetchStyle.SUBSELECT;
87+
};
8688
}
8789

8890
private static void validateFetchablePart(ModelPart fetchablePart, String profileName, Association association) {

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
import static java.util.Collections.emptySet;
140140
import static java.util.Collections.unmodifiableSet;
141141
import static org.hibernate.cfg.AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS;
142-
import static org.hibernate.internal.FetchProfileHelper.getFetchProfiles;
142+
import static org.hibernate.internal.FetchProfileHelper.addFetchProfiles;
143143
import static org.hibernate.internal.SessionFactorySettings.deprecationCheck;
144144
import static org.hibernate.internal.SessionFactorySettings.determineJndiName;
145145
import static org.hibernate.internal.SessionFactorySettings.getSessionFactoryName;
@@ -201,7 +201,6 @@ public class SessionFactoryImpl implements SessionFactoryImplementor, BindingCon
201201

202202
private final transient Map<String, FilterDefinition> filters;
203203
private final transient java.util.Collection<FilterDefinition> autoEnabledFilters = new HashSet<>();
204-
private final transient Map<String, FetchProfile> fetchProfiles;
205204
private final transient JavaType<Object> tenantIdentifierJavaType;
206205

207206
private final transient EventListenerGroups eventListenerGroups;
@@ -305,7 +304,8 @@ public SessionFactoryImpl(
305304
// and SqmFunctionRegistry, instantiating just the
306305
// registry here, and doing the engine later
307306
queryEngine = new QueryEngineImpl( bootMetamodel, options, this, serviceRegistry, settings, name );
308-
sqlTranslationEngine = new SqlTranslationEngineImpl( this, typeConfiguration );
307+
final Map<String, FetchProfile> fetchProfiles = new HashMap<>();
308+
sqlTranslationEngine = new SqlTranslationEngineImpl( this, typeConfiguration, fetchProfiles );
309309

310310
// create runtime metamodels (mapping and JPA)
311311
final RuntimeMetamodelsImpl runtimeMetamodelsImpl = new RuntimeMetamodelsImpl();
@@ -318,7 +318,7 @@ public SessionFactoryImpl(
318318

319319
// this needs to happen after the mapping metamodel is
320320
// completely built, since we need to use the persisters
321-
fetchProfiles = getFetchProfiles( bootMetamodel, runtimeMetamodelsImpl );
321+
addFetchProfiles( bootMetamodel, runtimeMetamodelsImpl, fetchProfiles );
322322

323323
defaultSessionOpenOptions = createDefaultSessionOpenOptionsIfPossible();
324324
temporarySessionOpenOptions = defaultSessionOpenOptions == null ? null : buildTemporarySessionOpenOptions();
@@ -1035,18 +1035,23 @@ public java.util.Collection<FilterDefinition> getAutoEnabledFilters() {
10351035
}
10361036

10371037
@Override
1038-
public boolean containsFetchProfileDefinition(String name) {
1039-
return fetchProfiles.containsKey( name );
1038+
public Set<String> getDefinedFilterNames() {
1039+
return unmodifiableSet( filters.keySet() );
10401040
}
10411041

10421042
@Override
1043-
public Set<String> getDefinedFilterNames() {
1044-
return unmodifiableSet( filters.keySet() );
1043+
public FetchProfile getFetchProfile(String name) {
1044+
return sqlTranslationEngine.getFetchProfile( name );
1045+
}
1046+
1047+
@Override
1048+
public boolean containsFetchProfileDefinition(String name) {
1049+
return sqlTranslationEngine.containsFetchProfileDefinition( name );
10451050
}
10461051

10471052
@Override
10481053
public Set<String> getDefinedFetchProfileNames() {
1049-
return unmodifiableSet( fetchProfiles.keySet() );
1054+
return sqlTranslationEngine.getDefinedFetchProfileNames();
10501055
}
10511056

10521057
@Override @Deprecated
@@ -1126,11 +1131,6 @@ public EntityNotFoundDelegate getEntityNotFoundDelegate() {
11261131
return sessionFactoryOptions.getEntityNotFoundDelegate();
11271132
}
11281133

1129-
@Override
1130-
public FetchProfile getFetchProfile(String name) {
1131-
return fetchProfiles.get( name );
1132-
}
1133-
11341134
/**
11351135
* @deprecated use {@link #configuredInterceptor(Interceptor, boolean, SessionFactoryOptions)}
11361136
*/

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ public SingleIdEntityLoaderStandardImpl(
3636
this(
3737
entityDescriptor,
3838
loadQueryInfluencers,
39-
(lockOptions, influencers) -> createLoadPlan( entityDescriptor, lockOptions, influencers, influencers.getSessionFactory() )
39+
(lockOptions, influencers) ->
40+
createLoadPlan( entityDescriptor, lockOptions, influencers, influencers.getSessionFactory() )
4041
);
4142
}
4243

hibernate-core/src/main/java/org/hibernate/query/sql/internal/SqlTranslationEngineImpl.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,31 @@
1111
import org.hibernate.query.sql.spi.SqlTranslationEngine;
1212
import org.hibernate.type.spi.TypeConfiguration;
1313

14+
import java.util.Map;
15+
import java.util.Set;
16+
17+
import static java.util.Collections.unmodifiableSet;
18+
1419
public class SqlTranslationEngineImpl implements SqlTranslationEngine {
1520

1621
//TODO: consider unifying with SqlStringGenerationContextImpl
1722

1823
private final SessionFactoryImplementor factory;
1924
private final TypeConfiguration typeConfiguration;
25+
private final Map<String, FetchProfile> fetchProfiles;
2026

21-
public SqlTranslationEngineImpl(SessionFactoryImplementor factory, TypeConfiguration typeConfiguration) {
27+
public SqlTranslationEngineImpl(
28+
SessionFactoryImplementor factory,
29+
TypeConfiguration typeConfiguration,
30+
Map<String, FetchProfile> fetchProfiles) {
2231
this.factory = factory;
2332
this.typeConfiguration = typeConfiguration;
33+
this.fetchProfiles = fetchProfiles;
34+
}
35+
36+
@Override
37+
public TypeConfiguration getTypeConfiguration() {
38+
return typeConfiguration;
2439
}
2540

2641
@Override
@@ -50,11 +65,16 @@ public JpaMetamodel getJpaMetamodel() {
5065

5166
@Override
5267
public FetchProfile getFetchProfile(String name) {
53-
return factory.getFetchProfile( name );
68+
return fetchProfiles.get( name );
5469
}
5570

5671
@Override
57-
public TypeConfiguration getTypeConfiguration() {
58-
return typeConfiguration;
72+
public boolean containsFetchProfileDefinition(String name) {
73+
return fetchProfiles.containsKey( name );
74+
}
75+
76+
@Override
77+
public Set<String> getDefinedFetchProfileNames() {
78+
return unmodifiableSet( fetchProfiles.keySet() );
5979
}
6080
}

hibernate-core/src/main/java/org/hibernate/query/sql/spi/SqlTranslationEngine.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import org.hibernate.Incubating;
88
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
99

10+
import java.util.Set;
11+
1012
/**
1113
* Introduced as an analog of {@link org.hibernate.query.spi.QueryEngine}
1214
* and/or {@link org.hibernate.query.sqm.NodeBuilder} for the SQL
@@ -19,4 +21,8 @@
1921
@Incubating
2022
public interface SqlTranslationEngine extends SqlAstCreationContext {
2123
// TODO: consider implementing SqlStringGenerationContext
24+
25+
boolean containsFetchProfileDefinition(String name);
26+
27+
Set<String> getDefinedFetchProfileNames();
2228
}

hibernate-core/src/main/java/org/hibernate/sql/ast/spi/SqlAstCreationContext.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,24 @@ public interface SqlAstCreationContext extends BindingContext {
5656
*/
5757
FetchProfile getFetchProfile(String name);
5858

59+
/**
60+
* Obtain the {@link SqmFunctionRegistry}.
61+
*/
5962
default SqmFunctionRegistry getSqmFunctionRegistry() {
6063
return getSessionFactory().getQueryEngine().getSqmFunctionRegistry();
6164
}
6265

66+
/**
67+
* Obtain the {@link Dialect}.
68+
*/
6369
default Dialect getDialect() {
6470
return getSessionFactory().getQueryEngine().getDialect();
6571
}
6672

73+
/**
74+
* Obtain the "incomplete" {@link WrapperOptions} that would be
75+
* returned by {@link SessionFactoryImplementor#getWrapperOptions()}.
76+
*/
6777
default WrapperOptions getWrapperOptions() {
6878
return getSessionFactory().getWrapperOptions();
6979
}

tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/MockSessionFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public Class<?> classForName(String className) {
239239

240240
nodeBuilder = new SqmCriteriaNodeBuilder("", "", this, this, this);
241241

242-
sqlTranslationEngine = new SqlTranslationEngineImpl(this, typeConfiguration);
242+
sqlTranslationEngine = new SqlTranslationEngineImpl(this, typeConfiguration, emptyMap() );
243243
}
244244

245245
@Override

0 commit comments

Comments
 (0)