11
11
import java .io .Serial ;
12
12
import java .sql .Connection ;
13
13
import java .util .ArrayList ;
14
+ import java .util .Collection ;
14
15
import java .util .HashMap ;
15
16
import java .util .HashSet ;
16
17
import java .util .List ;
93
94
import org .hibernate .metamodel .spi .RuntimeMetamodelsImplementor ;
94
95
import org .hibernate .metamodel .spi .RuntimeModelCreationContext ;
95
96
import org .hibernate .proxy .EntityNotFoundDelegate ;
96
- import org .hibernate .proxy .LazyInitializer ;
97
97
import org .hibernate .query .internal .QueryEngineImpl ;
98
98
import org .hibernate .query .named .NamedObjectRepository ;
99
99
import org .hibernate .query .spi .QueryEngine ;
@@ -192,7 +192,7 @@ public class SessionFactoryImpl implements SessionFactoryImplementor {
192
192
private final transient CurrentSessionContext currentSessionContext ;
193
193
194
194
private final transient Map <String , FilterDefinition > filters ;
195
- private final transient java . util . Collection <FilterDefinition > autoEnabledFilters = new HashSet <>();
195
+ private final transient Collection <FilterDefinition > autoEnabledFilters = new ArrayList <>();
196
196
private final transient JavaType <Object > tenantIdentifierJavaType ;
197
197
198
198
private final transient EventListenerGroups eventListenerGroups ;
@@ -251,19 +251,11 @@ public SessionFactoryImpl(
251
251
252
252
filters = new HashMap <>( bootMetamodel .getFilterDefinitions () );
253
253
254
- final var tenantFilter = filters .get ( TenantIdBinder .FILTER_NAME );
255
- if ( tenantFilter == null ) {
256
- tenantIdentifierJavaType = options .getDefaultTenantIdentifierJavaType ();
257
- }
258
- else {
259
- final var jdbcMapping = tenantFilter .getParameterJdbcMapping ( TenantIdBinder .PARAMETER_NAME );
260
- assert jdbcMapping != null ;
261
- //noinspection unchecked
262
- tenantIdentifierJavaType = jdbcMapping .getJavaTypeDescriptor ();
263
- }
264
- for ( var filterEntry : filters .entrySet () ) {
265
- if ( filterEntry .getValue ().isAutoEnabled () ) {
266
- autoEnabledFilters .add ( filterEntry .getValue () );
254
+ tenantIdentifierJavaType = tenantIdentifierType ( options );
255
+
256
+ for ( var filter : filters .values () ) {
257
+ if ( filter .isAutoEnabled () ) {
258
+ autoEnabledFilters .add ( filter );
267
259
}
268
260
}
269
261
@@ -357,6 +349,19 @@ public SessionFactoryImpl(
357
349
}
358
350
}
359
351
352
+ private JavaType <Object > tenantIdentifierType (SessionFactoryOptions options ) {
353
+ final var tenantFilter = filters .get ( TenantIdBinder .FILTER_NAME );
354
+ if ( tenantFilter == null ) {
355
+ return options .getDefaultTenantIdentifierJavaType ();
356
+ }
357
+ else {
358
+ final var jdbcMapping = tenantFilter .getParameterJdbcMapping ( TenantIdBinder .PARAMETER_NAME );
359
+ assert jdbcMapping != null ;
360
+ //noinspection unchecked
361
+ return jdbcMapping .getJavaTypeDescriptor ();
362
+ }
363
+ }
364
+
360
365
private EventMonitor loadEventMonitor () {
361
366
final var eventMonitors = classLoaderService .loadJavaServices ( EventMonitor .class );
362
367
return eventMonitors .isEmpty () ? new EmptyEventMonitor () : eventMonitors .iterator ().next ();
@@ -642,36 +647,33 @@ public Session createEntityManager() {
642
647
return buildEntityManager ( SYNCHRONIZED , null );
643
648
}
644
649
645
- private < K , V > Session buildEntityManager (final SynchronizationType synchronizationType , final Map <K , V > map ) {
650
+ private Session buildEntityManager (SynchronizationType synchronizationType , Map <?,? > map ) {
646
651
assert status != Status .CLOSED ;
647
652
648
- SessionBuilderImplementor builder = withOptions ();
653
+ var builder = withOptions ();
649
654
builder .autoJoinTransactions ( synchronizationType == SYNCHRONIZED );
650
655
651
656
if ( map != null ) {
652
- //noinspection SuspiciousMethodCalls
653
657
final Object tenantIdHint = map .get ( HINT_TENANT_ID );
654
658
if ( tenantIdHint != null ) {
655
- builder = ( SessionBuilderImplementor ) builder .tenantIdentifier ( tenantIdHint );
659
+ builder = builder .tenantIdentifier ( tenantIdHint );
656
660
}
657
661
}
658
662
659
- final Session session = builder .openSession ();
663
+ final var session = builder .openSession ();
660
664
if ( map != null ) {
661
- for ( Map .Entry <K , V > o : map .entrySet () ) {
662
- final K key = o .getKey ();
663
- if ( key instanceof String string ) {
665
+ for ( var entry : map .entrySet () ) {
666
+ if ( entry .getKey () instanceof String string ) {
664
667
if ( !HINT_TENANT_ID .equals ( string ) ) {
665
- session .setProperty ( string , o .getValue () );
668
+ session .setProperty ( string , entry .getValue () );
666
669
}
667
670
}
668
671
}
669
672
}
670
673
return session ;
671
674
}
672
675
673
- @ Override @ SuppressWarnings ("unchecked" )
674
- public Session createEntityManager (Map map ) {
676
+ public Session createEntityManager (Map <?,?> map ) {
675
677
validateNotClosed ();
676
678
return buildEntityManager ( SYNCHRONIZED , map );
677
679
}
@@ -695,8 +697,8 @@ private void errorIfResourceLocalDueToExplicitSynchronizationType() {
695
697
}
696
698
}
697
699
698
- @ Override @ SuppressWarnings ( "unchecked" )
699
- public Session createEntityManager (SynchronizationType synchronizationType , Map map ) {
700
+ @ Override
701
+ public Session createEntityManager (SynchronizationType synchronizationType , Map <?,?> map ) {
700
702
validateNotClosed ();
701
703
errorIfResourceLocalDueToExplicitSynchronizationType ();
702
704
return buildEntityManager ( synchronizationType , map );
@@ -737,7 +739,7 @@ public RootGraphImplementor<?> findEntityGraphByName(String name) {
737
739
738
740
@ Override
739
741
public String bestGuessEntityName (Object object ) {
740
- final LazyInitializer initializer = extractLazyInitializer ( object );
742
+ final var initializer = extractLazyInitializer ( object );
741
743
if ( initializer != null ) {
742
744
// it is possible for this method to be called during flush processing,
743
745
// so make certain that we do not accidentally initialize an uninitialized proxy
@@ -803,8 +805,8 @@ public void close() {
803
805
LOG .closingFactory ( getUuid () );
804
806
observer .sessionFactoryClosing ( this );
805
807
806
- // NOTE : the null checks below handle cases where close is called from
807
- // a failed attempt to create the SessionFactory
808
+ // NOTE: the null checks below handle cases where close is called
809
+ // from a failed attempt to create the SessionFactory
808
810
809
811
if ( cacheAccess != null ) {
810
812
cacheAccess .close ();
@@ -962,7 +964,7 @@ public FilterDefinition getFilterDefinition(String filterName) {
962
964
}
963
965
964
966
@ Override
965
- public java . util . Collection <FilterDefinition > getAutoEnabledFilters () {
967
+ public Collection <FilterDefinition > getAutoEnabledFilters () {
966
968
return autoEnabledFilters ;
967
969
}
968
970
@@ -1072,10 +1074,10 @@ public static Interceptor configuredInterceptor(Interceptor interceptor, Session
1072
1074
}
1073
1075
1074
1076
public static Interceptor configuredInterceptor (Interceptor interceptor , boolean explicitNoInterceptor , SessionFactoryOptions options ) {
1075
- // NOTE : DO NOT return EmptyInterceptor.INSTANCE from here as a "default for the Session"
1076
- // we "filter" that one out here. The return from here should represent the
1077
- // explicitly configured Interceptor (if one). Return null from here instead; Session
1078
- // will handle it
1077
+ // NOTE: DO NOT return EmptyInterceptor.INSTANCE from here as a "default for the Session"
1078
+ // we "filter" that one out here. The return from here should represent the
1079
+ // explicitly configured Interceptor (if one). Return null from here instead;
1080
+ // Session will handle it
1079
1081
1080
1082
if ( interceptor != null && interceptor != EmptyInterceptor .INSTANCE ) {
1081
1083
return interceptor ;
@@ -1087,7 +1089,7 @@ public static Interceptor configuredInterceptor(Interceptor interceptor, boolean
1087
1089
return optionsInterceptor ;
1088
1090
}
1089
1091
1090
- // If explicitly asking for no interceptor and there is no SessionFactory-scoped interceptors , then
1092
+ // If explicitly asking for no interceptor and there is no SessionFactory-scoped interceptor , then
1091
1093
// no need to inherit from the configured stateless session ones.
1092
1094
if ( explicitNoInterceptor ) {
1093
1095
return null ;
@@ -1349,7 +1351,7 @@ public SessionBuilderImpl eventListeners(SessionEventListener... listeners) {
1349
1351
public SessionBuilderImpl clearEventListeners () {
1350
1352
if ( listeners == null ) {
1351
1353
//Needs to initialize explicitly to an empty list as otherwise "null" implies the default listeners will be applied
1352
- this . listeners = new ArrayList <>( 3 );
1354
+ listeners = new ArrayList <>( 3 );
1353
1355
}
1354
1356
else {
1355
1357
listeners .clear ();
0 commit comments