HHH-18146 Switch to global temporary tables on H2

This commit is contained in:
Andrea Boriero 2024-05-27 14:50:27 +02:00 committed by Christian Beikov
parent 753076bb40
commit 78cf552065
5 changed files with 14 additions and 17 deletions

View File

@ -47,9 +47,8 @@ import org.hibernate.persister.entity.mutation.EntityMutationTarget;
import org.hibernate.query.sqm.FetchClauseType; import org.hibernate.query.sqm.FetchClauseType;
import org.hibernate.query.sqm.IntervalType; import org.hibernate.query.sqm.IntervalType;
import org.hibernate.query.sqm.TemporalUnit; import org.hibernate.query.sqm.TemporalUnit;
import org.hibernate.query.sqm.mutation.internal.temptable.BeforeUseAction; import org.hibernate.query.sqm.mutation.internal.temptable.GlobalTemporaryTableInsertStrategy;
import org.hibernate.query.sqm.mutation.internal.temptable.LocalTemporaryTableInsertStrategy; import org.hibernate.query.sqm.mutation.internal.temptable.GlobalTemporaryTableMutationStrategy;
import org.hibernate.query.sqm.mutation.internal.temptable.LocalTemporaryTableMutationStrategy;
import org.hibernate.query.sqm.mutation.spi.SqmMultiTableInsertStrategy; import org.hibernate.query.sqm.mutation.spi.SqmMultiTableInsertStrategy;
import org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy; import org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
@ -675,7 +674,7 @@ public class H2Dialect extends Dialect {
public SqmMultiTableMutationStrategy getFallbackSqmMutationStrategy( public SqmMultiTableMutationStrategy getFallbackSqmMutationStrategy(
EntityMappingType entityDescriptor, EntityMappingType entityDescriptor,
RuntimeModelCreationContext runtimeModelCreationContext) { RuntimeModelCreationContext runtimeModelCreationContext) {
return new LocalTemporaryTableMutationStrategy( return new GlobalTemporaryTableMutationStrategy(
TemporaryTable.createIdTable( TemporaryTable.createIdTable(
entityDescriptor, entityDescriptor,
basename -> TemporaryTable.ID_TABLE_PREFIX + basename, basename -> TemporaryTable.ID_TABLE_PREFIX + basename,
@ -690,7 +689,7 @@ public class H2Dialect extends Dialect {
public SqmMultiTableInsertStrategy getFallbackSqmInsertStrategy( public SqmMultiTableInsertStrategy getFallbackSqmInsertStrategy(
EntityMappingType entityDescriptor, EntityMappingType entityDescriptor,
RuntimeModelCreationContext runtimeModelCreationContext) { RuntimeModelCreationContext runtimeModelCreationContext) {
return new LocalTemporaryTableInsertStrategy( return new GlobalTemporaryTableInsertStrategy(
TemporaryTable.createEntityTable( TemporaryTable.createEntityTable(
entityDescriptor, entityDescriptor,
name -> TemporaryTable.ENTITY_TABLE_PREFIX + name, name -> TemporaryTable.ENTITY_TABLE_PREFIX + name,
@ -708,12 +707,7 @@ public class H2Dialect extends Dialect {
@Override @Override
public TemporaryTableKind getSupportedTemporaryTableKind() { public TemporaryTableKind getSupportedTemporaryTableKind() {
return TemporaryTableKind.LOCAL; return TemporaryTableKind.GLOBAL;
}
@Override
public BeforeUseAction getTemporaryTableBeforeUseAction() {
return BeforeUseAction.CREATE;
} }
@Override @Override

View File

@ -22,9 +22,6 @@ import org.hibernate.metamodel.mapping.internal.MappingModelCreationProcess;
public class LocalTemporaryTableStrategy { public class LocalTemporaryTableStrategy {
public static final String SHORT_NAME = "local_temporary"; public static final String SHORT_NAME = "local_temporary";
/**
* For H2 dialect avoid setting the drop strategy to true because H2 forces a commit when dropping a temporary table
*/
public static final String DROP_ID_TABLES = "hibernate.query.mutation_strategy.local_temporary.drop_tables"; public static final String DROP_ID_TABLES = "hibernate.query.mutation_strategy.local_temporary.drop_tables";
private final TemporaryTable temporaryTable; private final TemporaryTable temporaryTable;

View File

@ -57,7 +57,7 @@ public class DialectTempTableNoCommitTest {
@ServiceRegistry( @ServiceRegistry(
settings = @Setting(name = LocalTemporaryTableStrategy.DROP_ID_TABLES, value = "true") settings = @Setting(name = LocalTemporaryTableStrategy.DROP_ID_TABLES, value = "true")
) )
@SkipForDialect(dialectClass = H2Dialect.class) // @SkipForDialect(dialectClass = H2Dialect.class)
@SessionFactory @SessionFactory
public void noCommitAfterTempTableCreationAndDropTempTableTest(SessionFactoryScope scope) { public void noCommitAfterTempTableCreationAndDropTempTableTest(SessionFactoryScope scope) {
scope.inTransaction( scope.inTransaction(

View File

@ -21,6 +21,7 @@ import jakarta.persistence.PersistenceException;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.DerbyDialect;
import org.hibernate.dialect.H2Dialect; import org.hibernate.dialect.H2Dialect;
import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.engine.spi.SessionImplementor;
@ -47,7 +48,7 @@ public class DataSourceInjectionTest {
// otherwise the FakeDataSourceException will be eaten trying to resolve the Dialect // otherwise the FakeDataSourceException will be eaten trying to resolve the Dialect
final Map<String, Object> intgOverrides = Collections.singletonMap( final Map<String, Object> intgOverrides = Collections.singletonMap(
AvailableSettings.DIALECT, AvailableSettings.DIALECT,
H2Dialect.class DerbyDialect.class
); );
final HibernatePersistenceProvider provider = new HibernatePersistenceProvider(); final HibernatePersistenceProvider provider = new HibernatePersistenceProvider();

View File

@ -77,3 +77,8 @@ ORM 6.6 introduced support for `@Embeddable` type inheritance.
With it, we also enabled the `type()` and `treat()` functions to work with embeddable-typed paths. With it, we also enabled the `type()` and `treat()` functions to work with embeddable-typed paths.
As a consequence, the `SqmTreatedPath#getTreatTarget()` method will now return a generic `ManagedDomainType` object, As a consequence, the `SqmTreatedPath#getTreatTarget()` method will now return a generic `ManagedDomainType` object,
which could in turn be an `EntityDomainType` (as it was before) or also an `EmbeddableDomainType` instance. which could in turn be an `EntityDomainType` (as it was before) or also an `EmbeddableDomainType` instance.
[[h2-dialect]]
== H2 database and bulk mutation strategy
With ORM 6.6 when a bulk mutation involves multiple tables, H2 dialect will make use of global temporary tables instead of local ones.