more extreme solution

This commit is contained in:
Gavin King 2024-12-14 13:05:39 +01:00
parent a1b0fac8f1
commit 9a0948a32c
5 changed files with 20 additions and 127 deletions

View File

@ -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<Object> javaType = (JavaType<Object>) 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( '<' );

View File

@ -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
);
}
return literal.getJdbcMapping().getJdbcLiteralFormatter()
.toJdbcLiteral( literal.getLiteralValue(), sessionFactory.getJdbcServices().getDialect(),
sessionFactory.getWrapperOptions() );
}
else if ( expression instanceof ColumnReference columnReference ) {
return columnReference.getExpressionText();

View File

@ -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.
* <p>
* This object is {@link AutoCloseable}, and <em>must</em> 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() );
}
}

View File

@ -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<T extends JdbcOperation> implemen
// See #visitCteContainer for details about the usage.
private int withClauseRecursiveIndex = -1;
private transient FunctionRenderer castFunction;
private transient LazySessionWrapperOptions lazySessionWrapperOptions;
private transient BasicType<Integer> integerType;
private transient BasicType<String> stringType;
private transient BasicType<Boolean> booleanType;
@ -373,10 +371,7 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
}
protected WrapperOptions getWrapperOptions() {
if ( lazySessionWrapperOptions == null ) {
lazySessionWrapperOptions = new LazySessionWrapperOptions( sessionFactory );
}
return lazySessionWrapperOptions;
return sessionFactory.getWrapperOptions();
}
public BasicType<Integer> getIntegerType() {
@ -419,10 +414,6 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
}
protected void cleanup() {
if ( lazySessionWrapperOptions != null ) {
lazySessionWrapperOptions.cleanup();
lazySessionWrapperOptions = null;
}
this.jdbcParameterBindings = null;
this.lockOptions = null;
this.limit = null;

View File

@ -19,24 +19,27 @@ public abstract class BasicJdbcLiteralFormatter<T> extends AbstractJdbcLiteralFo
}
@SuppressWarnings("unchecked")
protected <X> X unwrap(Object value, Class<X> unwrapType, WrapperOptions wrapperOptions) {
protected <X> X unwrap(Object value, Class<X> 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<T> javaType = getJavaType();
if ( !javaType.isInstance( value ) ) {
final T coerced = javaType.coerce( value, () -> options.getSessionFactory().getTypeConfiguration() );
if ( unwrapType.isInstance( coerced ) ) {
return (X) coerced;
}
else {
return getJavaType().unwrap( coerce, unwrapType, wrapperOptions );
return javaType.unwrap( coerced, unwrapType, options );
}
}
else {
return getJavaType().unwrap( (T) value, unwrapType, wrapperOptions );
return javaType.unwrap( (T) value, unwrapType, options );
}
}
}
}