Skip to content

Commit

Permalink
[hibernate#2055] Add test for Setting hibernate.query.mutation_strate…
Browse files Browse the repository at this point in the history
…gy.global_temporary.create_tables to false has no effect
  • Loading branch information
dreab8 committed Feb 5, 2025
1 parent 634d072 commit 9819c9e
Showing 1 changed file with 125 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/* Hibernate, Relational Persistence for Idiomatic Java
*
* SPDX-License-Identifier: Apache-2.0
* Copyright: Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.reactive.schema;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.CompletionStage;

import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.temptable.TemporaryTable;
import org.hibernate.reactive.BaseReactiveTest;
import org.hibernate.reactive.testing.SqlStatementTracker;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import io.vertx.junit5.Timeout;
import io.vertx.junit5.VertxTestContext;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.SecondaryTable;
import jakarta.persistence.Table;

import static java.util.concurrent.TimeUnit.MINUTES;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hibernate.query.sqm.mutation.internal.temptable.GlobalTemporaryTableStrategy.CREATE_ID_TABLES;
import static org.hibernate.reactive.util.impl.CompletionStages.voidFuture;

@Timeout(value = 10, timeUnit = MINUTES)
public class GlobalTemporaryTableTest extends BaseReactiveTest {

private static SqlStatementTracker sqlTracker;

@Override
protected Collection<Class<?>> annotatedEntities() {
return List.of( Bar.class );
}


@Override
public CompletionStage<Void> deleteEntities(Class<?>... entities) {
// Skip delete step.
// We don't insert any value, so there's nothing to delete
// Avoid having extra stuff in the log that's not relevant to the test
return voidFuture();
}

@Override
protected void setProperties(Configuration configuration) {
super.setProperties( configuration );
configuration.setProperty( CREATE_ID_TABLES, "false" );
}

@Override
protected Configuration constructConfiguration() {
Configuration configuration = super.constructConfiguration();
sqlTracker = new SqlStatementTracker( GlobalTemporaryTableTest::filter, configuration.getProperties() );
return configuration;
}

@AfterEach
public void clearLogger() {
sqlTracker.clear();
}

@Override
protected void addServices(StandardServiceRegistryBuilder builder) {
sqlTracker.registerService( builder );
}


@Test
public void testGlobalTemporaryTableHaveNotBeenCreated(VertxTestContext context) {
test( context, getSessionFactory()
.withTransaction( session -> {
Bar bar = new Bar();
bar.id = 1;
bar.name = "Noble";
bar.name2 = "Experiment";

return session.persist( bar );
} )
.thenAccept( v -> {
List<String> loggedQueries = sqlTracker.getLoggedQueries();
assertThat( loggedQueries ).isNotEmpty();
loggedQueries.forEach( q -> {
assertThat( q ).doesNotContain( TemporaryTable.ID_TABLE_PREFIX ).as("");
assertThat( q ).doesNotContain( TemporaryTable.ENTITY_TABLE_PREFIX );
});

} )
);
}

private static boolean filter(String s) {
String[] accepted = { "create " };
for ( String valid : accepted ) {
if ( s.toLowerCase().startsWith( valid ) ) {
return true;
}
}
return false;
}

@Entity(name = "BAR")
@Table(name = "BAR")
@SecondaryTable(name = "BAR2")
public static class Bar {
@Id
@Column(name = "ID")
public Integer id;

@Column(name = "name")
public String name;

@Column(name = "name2", table = "BAR2")
public String name2;
}

}

0 comments on commit 9819c9e

Please sign in to comment.