Skip to content

Commit 2865d9f

Browse files
committed
various cleanups
1 parent 0a41a5d commit 2865d9f

14 files changed

+155
-211
lines changed

hibernate-core/src/main/java/org/hibernate/boot/internal/MetadataBuilderImpl.java

Lines changed: 45 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.util.ArrayList;
88
import java.util.List;
99
import java.util.Locale;
10-
import java.util.concurrent.Callable;
1110

1211
import org.hibernate.AnnotationException;
1312
import org.hibernate.HibernateException;
@@ -72,13 +71,10 @@
7271
import org.hibernate.internal.CoreLogging;
7372
import org.hibernate.internal.CoreMessageLogger;
7473
import org.hibernate.internal.log.DeprecationLogger;
75-
import org.hibernate.internal.util.NullnessHelper;
76-
import org.hibernate.internal.util.collections.CollectionHelper;
7774
import org.hibernate.metamodel.CollectionClassification;
7875
import org.hibernate.query.sqm.function.SqmFunctionDescriptor;
7976
import org.hibernate.query.sqm.function.SqmFunctionRegistry;
8077
import org.hibernate.service.ServiceRegistry;
81-
import org.hibernate.service.spi.ServiceException;
8278
import org.hibernate.type.BasicType;
8379
import org.hibernate.type.SqlTypes;
8480
import org.hibernate.type.WrapperArrayHandling;
@@ -95,7 +91,9 @@
9591
import static org.hibernate.cfg.AvailableSettings.WRAPPER_ARRAY_HANDLING;
9692
import static org.hibernate.cfg.MappingSettings.XML_FORMAT_MAPPER_LEGACY_FORMAT;
9793
import static org.hibernate.engine.config.spi.StandardConverters.BOOLEAN;
94+
import static org.hibernate.internal.util.NullnessHelper.coalesceSuppliedValues;
9895
import static org.hibernate.internal.util.StringHelper.nullIfEmpty;
96+
import static org.hibernate.internal.util.collections.CollectionHelper.isNotEmpty;
9997

10098
/**
10199
* @author Steve Ebersole
@@ -115,16 +113,15 @@ public static StandardServiceRegistry getStandardServiceRegistry(ServiceRegistry
115113
if ( serviceRegistry == null ) {
116114
throw new HibernateException( "ServiceRegistry passed to MetadataBuilder cannot be null" );
117115
}
118-
119-
if ( serviceRegistry instanceof StandardServiceRegistry ) {
120-
return (StandardServiceRegistry) serviceRegistry;
116+
else if ( serviceRegistry instanceof StandardServiceRegistry standardServiceRegistry ) {
117+
return standardServiceRegistry;
121118
}
122-
else if ( serviceRegistry instanceof BootstrapServiceRegistry ) {
119+
else if ( serviceRegistry instanceof BootstrapServiceRegistry bootstrapServiceRegistry ) {
123120
log.debug(
124121
"ServiceRegistry passed to MetadataBuilder was a BootstrapServiceRegistry; this likely won't end well " +
125122
"if attempt is made to build SessionFactory"
126123
);
127-
return new StandardServiceRegistryBuilder( (BootstrapServiceRegistry) serviceRegistry ).build();
124+
return new StandardServiceRegistryBuilder( bootstrapServiceRegistry ).build();
128125
}
129126
else {
130127
throw new HibernateException(
@@ -443,8 +440,9 @@ public MetadataImplementor build() {
443440

444441
final MetadataImplementor bootModel = MetadataBuildingProcess.build( sources, bootstrapContext, options );
445442

446-
if ( CollectionHelper.isNotEmpty( sources.getHbmXmlBindings() ) ) {
447-
final ConfigurationService configurationService = bootstrapContext.getServiceRegistry().getService( ConfigurationService.class );
443+
if ( isNotEmpty( sources.getHbmXmlBindings() ) ) {
444+
final ConfigurationService configurationService =
445+
bootstrapContext.getServiceRegistry().getService( ConfigurationService.class );
448446
final boolean transformHbm = configurationService != null
449447
&& configurationService.getSetting( MappingSettings.TRANSFORM_HBM_XML, BOOLEAN,false );
450448

@@ -750,19 +748,14 @@ else if ( value instanceof AccessType accessType ) {
750748
null
751749
) );
752750

753-
implicitNamingStrategy = strategySelector.resolveDefaultableStrategy(
751+
implicitNamingStrategy = strategySelector.<ImplicitNamingStrategy>resolveDefaultableStrategy(
754752
ImplicitNamingStrategy.class,
755753
configService.getSettings().get( AvailableSettings.IMPLICIT_NAMING_STRATEGY ),
756-
new Callable<>() {
757-
@Override
758-
public ImplicitNamingStrategy call() {
759-
return strategySelector.resolveDefaultableStrategy(
760-
ImplicitNamingStrategy.class,
761-
"default",
762-
ImplicitNamingStrategyJpaCompliantImpl.INSTANCE
763-
);
764-
}
765-
}
754+
() -> strategySelector.resolveDefaultableStrategy(
755+
ImplicitNamingStrategy.class,
756+
"default",
757+
ImplicitNamingStrategyJpaCompliantImpl.INSTANCE
758+
)
766759
);
767760

768761
physicalNamingStrategy = strategySelector.resolveDefaultableStrategy(
@@ -771,19 +764,14 @@ public ImplicitNamingStrategy call() {
771764
PhysicalNamingStrategyStandardImpl.INSTANCE
772765
);
773766

774-
columnOrderingStrategy = strategySelector.resolveDefaultableStrategy(
767+
columnOrderingStrategy = strategySelector.<ColumnOrderingStrategy>resolveDefaultableStrategy(
775768
ColumnOrderingStrategy.class,
776769
configService.getSettings().get( AvailableSettings.COLUMN_ORDERING_STRATEGY ),
777-
new Callable<>() {
778-
@Override
779-
public ColumnOrderingStrategy call() {
780-
return strategySelector.resolveDefaultableStrategy(
781-
ColumnOrderingStrategy.class,
782-
"default",
783-
ColumnOrderingStrategyStandard.INSTANCE
784-
);
785-
}
786-
}
770+
() -> strategySelector.resolveDefaultableStrategy(
771+
ColumnOrderingStrategy.class,
772+
"default",
773+
ColumnOrderingStrategyStandard.INSTANCE
774+
)
787775
);
788776

789777
useNationalizedCharacterData = configService.getSetting(
@@ -822,14 +810,7 @@ public TimeZoneStorageStrategy getDefaultTimeZoneStorage() {
822810

823811
@Override
824812
public TimeZoneSupport getTimeZoneSupport() {
825-
try {
826-
return serviceRegistry.requireService( JdbcServices.class )
827-
.getDialect()
828-
.getTimeZoneSupport();
829-
}
830-
catch ( ServiceException se ) {
831-
return TimeZoneSupport.NONE;
832-
}
813+
return getTypeConfiguration().getCurrentBaseSqlTypeIndicators().getDialect().getTimeZoneSupport();
833814
}
834815

835816
private TimeZoneStorageStrategy toTimeZoneStorageStrategy(TimeZoneSupport timeZoneSupport) {
@@ -844,20 +825,16 @@ private TimeZoneStorageStrategy toTimeZoneStorageStrategy(TimeZoneSupport timeZo
844825
case NORMALIZE -> TimeZoneStorageStrategy.NORMALIZE;
845826
case NORMALIZE_UTC -> TimeZoneStorageStrategy.NORMALIZE_UTC;
846827
case AUTO -> switch (timeZoneSupport) {
847-
case NATIVE ->
848-
// if the db has native support for timezones, we use that, not a column
849-
TimeZoneStorageStrategy.NATIVE;
850-
case NORMALIZE, NONE ->
851-
// otherwise we use a separate column
852-
TimeZoneStorageStrategy.COLUMN;
828+
// if the db has native support for timezones, we use that, not a column
829+
case NATIVE -> TimeZoneStorageStrategy.NATIVE;
830+
// otherwise we use a separate column
831+
case NORMALIZE, NONE -> TimeZoneStorageStrategy.COLUMN;
853832
};
854833
case DEFAULT -> switch (timeZoneSupport) {
855-
case NATIVE ->
856-
// if the db has native support for timezones, we use that, and don't normalize
857-
TimeZoneStorageStrategy.NATIVE;
858-
case NORMALIZE, NONE ->
859-
// otherwise we normalize things to UTC
860-
TimeZoneStorageStrategy.NORMALIZE_UTC;
834+
// if the db has native support for timezones, we use that, and don't normalize
835+
case NATIVE -> TimeZoneStorageStrategy.NATIVE;
836+
// otherwise we normalize things to UTC
837+
case NORMALIZE, NONE -> TimeZoneStorageStrategy.NORMALIZE_UTC;
861838
};
862839
};
863840
}
@@ -979,9 +956,7 @@ public void apply(JpaOrmXmlPersistenceUnitDefaults jpaOrmXmlPersistenceUnitDefau
979956
}
980957

981958
if ( mappingDefaults.getImplicitSchemaName() == null ) {
982-
mappingDefaults.implicitSchemaName = nullIfEmpty(
983-
jpaOrmXmlPersistenceUnitDefaults.getDefaultSchemaName()
984-
);
959+
mappingDefaults.implicitSchemaName = nullIfEmpty( jpaOrmXmlPersistenceUnitDefaults.getDefaultSchemaName() );
985960
}
986961
}
987962

@@ -1017,27 +992,28 @@ private static TimeZoneStorageType resolveTimeZoneStorageStrategy(
1017992
private static WrapperArrayHandling resolveWrapperArrayHandling(
1018993
ConfigurationService configService,
1019994
StandardServiceRegistry serviceRegistry) {
1020-
final WrapperArrayHandling setting = NullnessHelper.coalesceSuppliedValues(
995+
final WrapperArrayHandling setting = coalesceSuppliedValues(
1021996
() -> configService.getSetting(
1022997
WRAPPER_ARRAY_HANDLING,
1023998
WrapperArrayHandling::interpretExternalSettingLeniently
1024999
),
10251000
() -> resolveFallbackWrapperArrayHandling( configService )
10261001
);
10271002

1028-
if ( setting == WrapperArrayHandling.PICK ) {
1029-
final Dialect dialect = serviceRegistry.requireService( JdbcServices.class ).getDialect();
1030-
if ( dialect.supportsStandardArrays()
1031-
&& ( dialect.getPreferredSqlTypeCodeForArray() == SqlTypes.ARRAY
1032-
|| dialect.getPreferredSqlTypeCodeForArray() == SqlTypes.SQLXML ) ) {
1033-
return WrapperArrayHandling.ALLOW;
1034-
}
1035-
else {
1036-
return WrapperArrayHandling.LEGACY;
1037-
}
1038-
}
1003+
final Dialect dialect = serviceRegistry.requireService( JdbcServices.class ).getDialect();
1004+
return setting == WrapperArrayHandling.PICK ? pickWrapperArrayHandling( dialect ) : setting;
10391005

1040-
return setting;
1006+
}
1007+
1008+
private static WrapperArrayHandling pickWrapperArrayHandling(Dialect dialect) {
1009+
if ( dialect.supportsStandardArrays()
1010+
&& ( dialect.getPreferredSqlTypeCodeForArray() == SqlTypes.ARRAY
1011+
|| dialect.getPreferredSqlTypeCodeForArray() == SqlTypes.SQLXML ) ) {
1012+
return WrapperArrayHandling.ALLOW;
1013+
}
1014+
else {
1015+
return WrapperArrayHandling.LEGACY;
1016+
}
10411017
}
10421018

10431019
private static WrapperArrayHandling resolveFallbackWrapperArrayHandling(

hibernate-core/src/main/java/org/hibernate/dialect/XmlHelper.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,19 +1124,15 @@ public static CollectionTags determineCollectionTags(BasicPluralJavaType<?> plur
11241124
}
11251125
//noinspection unchecked
11261126
final JavaType<Object> javaType = (JavaType<Object>) pluralJavaType;
1127-
final LazySessionWrapperOptions lazySessionWrapperOptions = new LazySessionWrapperOptions( sessionFactory );
11281127
// Produce the XML string for a collection with a null element to find out the root and element tag names
11291128
final String nullElementXml;
1130-
try {
1129+
try ( final LazySessionWrapperOptions lazySessionWrapperOptions = new LazySessionWrapperOptions( sessionFactory ) ) {
11311130
nullElementXml = sessionFactory.getSessionFactoryOptions().getXmlFormatMapper().toString(
11321131
javaType.fromString( "{null}" ),
11331132
javaType,
11341133
lazySessionWrapperOptions
11351134
);
11361135
}
1137-
finally {
1138-
lazySessionWrapperOptions.cleanup();
1139-
}
11401136

11411137
// There must be an end tag for the root, so find that first
11421138
final int rootCloseTagPosition = nullElementXml.lastIndexOf( '<' );

hibernate-core/src/main/java/org/hibernate/dialect/aggregate/OracleAggregateSupport.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ private static WrapperOptions getWrapperOptions(TypeConfiguration typeConfigurat
312312
return typeConfiguration.getSessionFactory().getWrapperOptions();
313313
}
314314
catch (HibernateException e) {
315+
// before we have a SessionFactory, no useful WrapperOptions to pass
315316
return null;
316317
}
317318
}
@@ -549,16 +550,11 @@ private String determineJsonTypeName(SelectableMapping aggregateColumn) {
549550
final String columnDefinition = aggregateColumn.getColumnDefinition();
550551
if ( columnDefinition == null ) {
551552
assert aggregateColumn.getJdbcMapping().getJdbcType().getDefaultSqlTypeCode() == JSON;
552-
switch ( jsonSupport ) {
553-
case OSON:
554-
return "json";
555-
case MERGEPATCH:
556-
case QUERY_AND_PATH:
557-
case QUERY:
558-
return "blob";
559-
case NONE:
560-
return "clob";
561-
}
553+
return switch ( jsonSupport ) {
554+
case OSON -> "json";
555+
case MERGEPATCH, QUERY_AND_PATH, QUERY -> "blob";
556+
case NONE -> "clob";
557+
};
562558
}
563559
return columnDefinition;
564560
}

hibernate-core/src/main/java/org/hibernate/dialect/function/NumberSeriesGenerateSeriesFunction.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -485,18 +485,14 @@ private String getStepExpression(@Nullable Expression explicitStep, String table
485485
private String getExpression(Expression expression, String tableIdentifierVariable, String syntheticColumnName, SqmToSqlAstConverter walker) {
486486
if ( expression instanceof Literal literal ) {
487487
final SessionFactoryImplementor sessionFactory = walker.getCreationContext().getSessionFactory();
488-
final LazySessionWrapperOptions wrapperOptions = new LazySessionWrapperOptions( sessionFactory );
489-
try {
488+
try ( final LazySessionWrapperOptions wrapperOptions = new LazySessionWrapperOptions( sessionFactory ) ) {
490489
//noinspection unchecked
491490
return literal.getJdbcMapping().getJdbcLiteralFormatter().toJdbcLiteral(
492491
literal.getLiteralValue(),
493492
sessionFactory.getJdbcServices().getDialect(),
494493
wrapperOptions
495494
);
496495
}
497-
finally {
498-
wrapperOptions.cleanup();
499-
}
500496
}
501497
else if ( expression instanceof ColumnReference columnReference ) {
502498
return columnReference.getExpressionText();

hibernate-core/src/main/java/org/hibernate/engine/spi/AbstractDelegatingWrapperOptions.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
/**
1313
* @author Christian Beikov
1414
* @author Andrea Boriero
15+
*
16+
* @deprecated This isn't bringing any value, since {@link SessionImplementor}
17+
* already extends {@link WrapperOptions}.
1518
*/
19+
@Deprecated(since = "7", forRemoval = true)
1620
public abstract class AbstractDelegatingWrapperOptions implements WrapperOptions {
1721

1822
/**

hibernate-core/src/main/java/org/hibernate/engine/spi/LazySessionWrapperOptions.java

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,37 @@
77
import java.util.TimeZone;
88

99
import org.hibernate.Internal;
10+
import org.hibernate.dialect.Dialect;
11+
import org.hibernate.engine.jdbc.LobCreator;
12+
import org.hibernate.internal.FastSessionServices;
1013
import org.hibernate.type.descriptor.WrapperOptions;
1114

1215
import org.checkerframework.checker.nullness.qual.Nullable;
1316

1417
/**
15-
* A lazy session implementation that is needed for rendering literals.
16-
* Usually, only the {@link WrapperOptions} interface is needed,
17-
* but for creating LOBs, it might be to have a full-blown session.
18+
* An implementation of {@link WrapperOptions} used for rendering SQL literals,
19+
* which is backed by the {@link SessionFactoryImplementor}, and which
20+
* {@linkplain SessionFactoryImplementor#openTemporarySession lazily creates a
21+
* temporary session if needed.} The temporary session will only be created when
22+
* dealing with LOBs.
23+
* <p>
24+
* This object is {@link AutoCloseable}, and <em>must</em> be explicitly cleaned
25+
* up by its creator.
26+
*
27+
* @apiNote This thing is nasty, and we should find a better way to solve the problem.
28+
* Whenever possible, just use {@link SessionFactoryImplementor#getWrapperOptions()}
29+
* instead.
1830
*/
1931
@Internal
20-
public class LazySessionWrapperOptions extends AbstractDelegatingWrapperOptions {
32+
public class LazySessionWrapperOptions implements WrapperOptions, AutoCloseable {
2133

2234
private final SessionFactoryImplementor sessionFactory;
35+
private final FastSessionServices fastSessionServices;
2336
private @Nullable SessionImplementor session;
2437

2538
public LazySessionWrapperOptions(SessionFactoryImplementor sessionFactory) {
2639
this.sessionFactory = sessionFactory;
40+
fastSessionServices = sessionFactory.getFastSessionServices();
2741
}
2842

2943
public void cleanup() {
@@ -34,16 +48,14 @@ public void cleanup() {
3448
}
3549

3650
@Override
37-
protected SessionImplementor delegate() {
38-
if ( session == null ) {
39-
session = sessionFactory.openTemporarySession();
40-
}
41-
return session;
51+
public void close() {
52+
cleanup();
4253
}
4354

4455
@Override
4556
public SharedSessionContractImplementor getSession() {
46-
return delegate();
57+
session = sessionFactory.openTemporarySession();
58+
return session;
4759
}
4860

4961
@Override
@@ -53,16 +65,26 @@ public SessionFactoryImplementor getSessionFactory() {
5365

5466
@Override
5567
public boolean useStreamForLobBinding() {
56-
return sessionFactory.getFastSessionServices().useStreamForLobBinding();
68+
return fastSessionServices.useStreamForLobBinding;
5769
}
5870

5971
@Override
6072
public int getPreferredSqlTypeCodeForBoolean() {
61-
return sessionFactory.getFastSessionServices().getPreferredSqlTypeCodeForBoolean();
73+
return fastSessionServices.preferredSqlTypeCodeForBoolean;
6274
}
6375

6476
@Override
6577
public TimeZone getJdbcTimeZone() {
66-
return sessionFactory.getSessionFactoryOptions().getJdbcTimeZone();
78+
return fastSessionServices.jdbcTimeZone;
79+
}
80+
81+
@Override
82+
public Dialect getDialect() {
83+
return fastSessionServices.dialect;
84+
}
85+
86+
@Override
87+
public LobCreator getLobCreator() {
88+
return fastSessionServices.jdbcServices.getLobCreator( getSession() );
6789
}
6890
}

0 commit comments

Comments
 (0)