From 9a0948a32cdcd6ce16552240d65e9b3addcd9d9d Mon Sep 17 00:00:00 2001 From: Gavin King Date: Sat, 14 Dec 2024 13:05:39 +0100 Subject: [PATCH] more extreme solution --- .../java/org/hibernate/dialect/XmlHelper.java | 12 +-- .../NumberSeriesGenerateSeriesFunction.java | 13 +-- .../engine/spi/LazySessionWrapperOptions.java | 90 ------------------- .../sql/ast/spi/AbstractSqlAstTranslator.java | 11 +-- .../jdbc/spi/BasicJdbcLiteralFormatter.java | 21 +++-- 5 files changed, 20 insertions(+), 127 deletions(-) delete mode 100644 hibernate-core/src/main/java/org/hibernate/engine/spi/LazySessionWrapperOptions.java diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/XmlHelper.java b/hibernate-core/src/main/java/org/hibernate/dialect/XmlHelper.java index 1c924bf55b..047505e166 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/XmlHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/XmlHelper.java @@ -16,7 +16,6 @@ import java.util.List; import org.checkerframework.checker.nullness.qual.Nullable; import org.hibernate.Internal; -import org.hibernate.engine.spi.LazySessionWrapperOptions; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.internal.util.CharSequenceHelper; import org.hibernate.metamodel.mapping.EmbeddableMappingType; @@ -1125,14 +1124,9 @@ public class XmlHelper { //noinspection unchecked final JavaType javaType = (JavaType) pluralJavaType; // Produce the XML string for a collection with a null element to find out the root and element tag names - final String nullElementXml; - try ( final LazySessionWrapperOptions lazySessionWrapperOptions = new LazySessionWrapperOptions( sessionFactory ) ) { - nullElementXml = sessionFactory.getSessionFactoryOptions().getXmlFormatMapper().toString( - javaType.fromString( "{null}" ), - javaType, - lazySessionWrapperOptions - ); - } + final String nullElementXml = + sessionFactory.getSessionFactoryOptions().getXmlFormatMapper() + .toString( javaType.fromString( "{null}" ), javaType, sessionFactory.getWrapperOptions() ); // There must be an end tag for the root, so find that first final int rootCloseTagPosition = nullElementXml.lastIndexOf( '<' ); diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/function/NumberSeriesGenerateSeriesFunction.java b/hibernate-core/src/main/java/org/hibernate/dialect/function/NumberSeriesGenerateSeriesFunction.java index 1b89d24bce..fc81fa4e17 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/function/NumberSeriesGenerateSeriesFunction.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/function/NumberSeriesGenerateSeriesFunction.java @@ -5,7 +5,6 @@ package org.hibernate.dialect.function; import org.checkerframework.checker.nullness.qual.Nullable; -import org.hibernate.engine.spi.LazySessionWrapperOptions; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.internal.util.NullnessHelper; import org.hibernate.metamodel.mapping.BasicValuedMapping; @@ -485,14 +484,10 @@ public abstract class NumberSeriesGenerateSeriesFunction extends GenerateSeriesF private String getExpression(Expression expression, String tableIdentifierVariable, String syntheticColumnName, SqmToSqlAstConverter walker) { if ( expression instanceof Literal literal ) { final SessionFactoryImplementor sessionFactory = walker.getCreationContext().getSessionFactory(); - try ( final LazySessionWrapperOptions wrapperOptions = new LazySessionWrapperOptions( sessionFactory ) ) { - //noinspection unchecked - return literal.getJdbcMapping().getJdbcLiteralFormatter().toJdbcLiteral( - literal.getLiteralValue(), - sessionFactory.getJdbcServices().getDialect(), - wrapperOptions - ); - } + //noinspection unchecked + return literal.getJdbcMapping().getJdbcLiteralFormatter() + .toJdbcLiteral( literal.getLiteralValue(), sessionFactory.getJdbcServices().getDialect(), + sessionFactory.getWrapperOptions() ); } else if ( expression instanceof ColumnReference columnReference ) { return columnReference.getExpressionText(); diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/LazySessionWrapperOptions.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/LazySessionWrapperOptions.java deleted file mode 100644 index ace47cd84b..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/LazySessionWrapperOptions.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * SPDX-License-Identifier: LGPL-2.1-or-later - * Copyright Red Hat Inc. and Hibernate Authors - */ -package org.hibernate.engine.spi; - -import java.util.TimeZone; - -import org.hibernate.Internal; -import org.hibernate.dialect.Dialect; -import org.hibernate.engine.jdbc.LobCreator; -import org.hibernate.internal.FastSessionServices; -import org.hibernate.type.descriptor.WrapperOptions; - -import org.checkerframework.checker.nullness.qual.Nullable; - -/** - * An implementation of {@link WrapperOptions} used for rendering SQL literals, - * which is backed by the {@link SessionFactoryImplementor}, and which - * {@linkplain SessionFactoryImplementor#openTemporarySession lazily creates a - * temporary session if needed.} The temporary session will only be created when - * dealing with LOBs. - *

- * This object is {@link AutoCloseable}, and must be explicitly cleaned - * up by its creator. - * - * @apiNote This thing is nasty, and we should find a better way to solve the problem. - * Whenever possible, just use {@link SessionFactoryImplementor#getWrapperOptions()} - * instead. - */ -@Internal -public class LazySessionWrapperOptions implements WrapperOptions, AutoCloseable { - - private final SessionFactoryImplementor sessionFactory; - private final FastSessionServices fastSessionServices; - private @Nullable SessionImplementor session; - - public LazySessionWrapperOptions(SessionFactoryImplementor sessionFactory) { - this.sessionFactory = sessionFactory; - fastSessionServices = sessionFactory.getFastSessionServices(); - } - - public void cleanup() { - if ( session != null ) { - session.close(); - session = null; - } - } - - @Override - public void close() { - cleanup(); - } - - @Override - public SharedSessionContractImplementor getSession() { - session = sessionFactory.openTemporarySession(); - return session; - } - - @Override - public SessionFactoryImplementor getSessionFactory() { - return sessionFactory; - } - - @Override - public boolean useStreamForLobBinding() { - return fastSessionServices.useStreamForLobBinding; - } - - @Override - public int getPreferredSqlTypeCodeForBoolean() { - return fastSessionServices.preferredSqlTypeCodeForBoolean; - } - - @Override - public TimeZone getJdbcTimeZone() { - return fastSessionServices.jdbcTimeZone; - } - - @Override - public Dialect getDialect() { - return fastSessionServices.dialect; - } - - @Override - public LobCreator getLobCreator() { - return fastSessionServices.jdbcServices.getLobCreator( getSession() ); - } -} diff --git a/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstTranslator.java b/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstTranslator.java index d9096e88e5..12e8ce9d11 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstTranslator.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstTranslator.java @@ -32,7 +32,6 @@ import org.hibernate.dialect.RowLockStrategy; import org.hibernate.dialect.SelectItemReferenceStrategy; import org.hibernate.engine.jdbc.Size; import org.hibernate.engine.jdbc.spi.JdbcServices; -import org.hibernate.engine.spi.LazySessionWrapperOptions; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.internal.FilterJdbcParameter; import org.hibernate.internal.util.MathHelper; @@ -330,7 +329,6 @@ public abstract class AbstractSqlAstTranslator implemen // See #visitCteContainer for details about the usage. private int withClauseRecursiveIndex = -1; private transient FunctionRenderer castFunction; - private transient LazySessionWrapperOptions lazySessionWrapperOptions; private transient BasicType integerType; private transient BasicType stringType; private transient BasicType booleanType; @@ -373,10 +371,7 @@ public abstract class AbstractSqlAstTranslator implemen } protected WrapperOptions getWrapperOptions() { - if ( lazySessionWrapperOptions == null ) { - lazySessionWrapperOptions = new LazySessionWrapperOptions( sessionFactory ); - } - return lazySessionWrapperOptions; + return sessionFactory.getWrapperOptions(); } public BasicType getIntegerType() { @@ -419,10 +414,6 @@ public abstract class AbstractSqlAstTranslator implemen } protected void cleanup() { - if ( lazySessionWrapperOptions != null ) { - lazySessionWrapperOptions.cleanup(); - lazySessionWrapperOptions = null; - } this.jdbcParameterBindings = null; this.lockOptions = null; this.limit = null; diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/spi/BasicJdbcLiteralFormatter.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/spi/BasicJdbcLiteralFormatter.java index 390151e5b0..9ecce5f6a5 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/spi/BasicJdbcLiteralFormatter.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/spi/BasicJdbcLiteralFormatter.java @@ -19,24 +19,27 @@ public abstract class BasicJdbcLiteralFormatter extends AbstractJdbcLiteralFo } @SuppressWarnings("unchecked") - protected X unwrap(Object value, Class unwrapType, WrapperOptions wrapperOptions) { + protected X unwrap(Object value, Class unwrapType, WrapperOptions options) { assert value != null; // for performance reasons, avoid conversions if we can if ( unwrapType.isInstance( value ) ) { return (X) value; } - else if ( !getJavaType().isInstance( value ) ) { - final T coerce = getJavaType().coerce( value, wrapperOptions.getSession() ); - if ( unwrapType.isInstance( coerce ) ) { - return (X) coerce; + else { + final JavaType javaType = getJavaType(); + if ( !javaType.isInstance( value ) ) { + final T coerced = javaType.coerce( value, () -> options.getSessionFactory().getTypeConfiguration() ); + if ( unwrapType.isInstance( coerced ) ) { + return (X) coerced; + } + else { + return javaType.unwrap( coerced, unwrapType, options ); + } } else { - return getJavaType().unwrap( coerce, unwrapType, wrapperOptions ); + return javaType.unwrap( (T) value, unwrapType, options ); } } - else { - return getJavaType().unwrap( (T) value, unwrapType, wrapperOptions ); - } } }