Skip to content

More tuncate() in tests #10378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public interface SchemaManager extends jakarta.persistence.SchemaManager {
* load script}.
* <p>
* Programmatic way to run {@link org.hibernate.tool.schema.spi.SchemaTruncator}.
* <p>
* This operation does not affect the {@linkplain org.hibernate.Cache second-level cache}.
* Therefore, after calling {@code truncate()}, it might be necessary to also call
* {@link org.hibernate.Cache#evictAllRegions} to clean up data held in the second-level
* cache.
*
* @apiNote This operation is a synonym for {@link #truncate}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,8 @@ public void prepareTestData(SessionFactoryScope scope) {

@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createMutationQuery("delete from CacheHolder").executeUpdate();
session.createMutationQuery( "delete Cacheable" ).executeUpdate();
}
);
scope.getSessionFactory().getSchemaManager().truncate();
scope.getSessionFactory().getCache().evictAll();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,7 @@ protected void prepareBootstrapRegistryBuilder(BootstrapServiceRegistryBuilder b

@Override
protected void cleanupTestData() throws Exception {
Session s = openSession();
s.beginTransaction();
List<Alias> aliases = s.createQuery( "from Alias" ).list();
for ( Alias alias : aliases ) {
for ( Character character : alias.getCharacters() ) {
character.getAliases().clear();
}
alias.getCharacters().clear();
}
s.flush();
s.createQuery( "delete Alias" ).executeUpdate();
s.createQuery( "delete Character" ).executeUpdate();
s.getTransaction().commit();
s.close();
sessionFactory().getSchemaManager().truncate();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,7 @@ public void testBatchingAmongstSubClasses() {

@After
protected void cleanupTestData() {
sessionFactoryScope().inTransaction( session -> {
session.createMutationQuery( "delete Address" ).executeUpdate();
session.createMutationQuery( "delete Person" ).executeUpdate();
session.createMutationQuery( "delete SpecialPerson" ).executeUpdate();
session.createMutationQuery( "delete AnotherPerson" ).executeUpdate();
session.createMutationQuery( "delete Office" ).executeUpdate();
session.createMutationQuery( "delete President" ).executeUpdate();
} );
sessionFactoryScope().getSessionFactory().getSchemaManager().truncate();
}

@Entity(name = "Address")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.community.dialect.AltibaseDialect;
import org.hibernate.dialect.DB2Dialect;
import org.hibernate.community.dialect.DerbyDialect;
Expand Down Expand Up @@ -78,12 +79,7 @@ public void prepareTestData() {

@AfterEach
public void cleanupTestData() {
doInJPA(
this::entityManagerFactory,
entityManager -> {
entityManager.createQuery( "delete from Product" ).executeUpdate();
}
);
entityManagerFactory().unwrap(SessionFactory.class).getSchemaManager().truncate();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
*
* @author Steve Ebersole
*/
@SuppressWarnings("JUnitMalformedDeclaration")
public class CharEnumerateValueTests {
@Test
@DomainModel(annotatedClasses = Person.class)
Expand Down Expand Up @@ -122,7 +121,7 @@ void verifyCheckConstraints2(SessionFactoryScope scope) {

@AfterEach
void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> session.createMutationQuery( "delete Person" ).executeUpdate() );
scope.getSessionFactory().getSchemaManager().truncate();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder ... what if we add something like

public @interface SessionFactory {
	boolean truncate() default true; 
....

And do it as part of the extension rather than in the tests? (then we could skip a lot of these after each calls ...)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marko-bekhta Yeah, that would be great.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I gave it a try (truncate after each, evict cache after each):

15901 tests

756 failures

🙈 🥲

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weird.

Copy link
Member

@marko-bekhta marko-bekhta Jun 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here's that experiment: #10380

from a few tests I've checked they either want to do something witch cache or rely on BeforeAll to setup the test data ...

(I set the defaults to NEVER in that PR so it should pass the tests ... but if we want to make the cleanups more automatic, we'd probably have to adjust those failing tests 😃 )

}

public enum Gender {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void verifyCheckConstraints(SessionFactoryScope scope) {

@AfterEach
void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> session.createMutationQuery( "delete Person" ).executeUpdate() );
scope.getSessionFactory().getSchemaManager().truncate();
}

public enum Gender {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void verifyCheckConstraints2(SessionFactoryScope scope) {

@AfterEach
void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> session.createMutationQuery( "delete Person" ).executeUpdate() );
scope.getSessionFactory().getSchemaManager().truncate();
}

public enum Gender {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,7 @@ public void prepareTestData(SessionFactoryScope scope) {

@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
session.createQuery( "delete EmployeeGroup" ).executeUpdate();
session.createQuery( "delete Employee" ).executeUpdate();
} );
scope.getSessionFactory().getSchemaManager().truncate();
}

@Entity( name="EmployeeGroup")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,7 @@ public void prepareTestData(SessionFactoryScope scope) {

@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
session.createQuery( "delete EmployeeGroup" ).executeUpdate();
session.createQuery( "delete Employee" ).executeUpdate();
} );
scope.getSessionFactory().getSchemaManager().truncate();
}

@Entity( name="EmployeeGroup")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@ public void prepareTestData(SessionFactoryScope scope) {

@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
session.createQuery( "delete Thing" ).executeUpdate();
session.createQuery( "delete Owner" ).executeUpdate();
} );
scope.getSessionFactory().getSchemaManager().truncate();
}

@Entity(name = "Owner")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,7 @@ public void prepareTestData(SessionFactoryScope scope) {

@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
session.createQuery( "delete Thing" ).executeUpdate();
session.createQuery( "delete Owner" ).executeUpdate();
} );
scope.getSessionFactory().getSchemaManager().truncate();
}

@Entity(name = "Owner")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,7 @@ public void prepareTestData(SessionFactoryScope scope) {

@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
session.createQuery( "delete Thing" ).executeUpdate();
session.createQuery( "delete Owner" ).executeUpdate();
} );
scope.getSessionFactory().getSchemaManager().truncate();
}

@Entity(name = "Owner")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,7 @@ public void prepareTestData(SessionFactoryScope scope) {

@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
session.createQuery( "delete Thing" ).executeUpdate();
session.createQuery( "delete Owner" ).executeUpdate();
} );
scope.getSessionFactory().getSchemaManager().truncate();
}

@Entity(name = "Owner")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ void testCriteria(SessionFactoryScope scope) {

@AfterEach
void tearDown(SessionFactoryScope scope) {
scope.inTransaction( session -> {
session.createQuery( "delete from Account" ).executeUpdate();
} );
scope.getSessionFactory().getSchemaManager().truncate();
}

@Entity(name = "Account")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void setUp(SessionFactoryScope scope) {

@AfterEach
void tearDown(SessionFactoryScope scope) {
scope.inTransaction( session -> session.createQuery( "delete from Foo" ).executeUpdate() );
scope.getSessionFactory().getSchemaManager().truncate();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ void testCriteria(SessionFactoryScope scope) {

@AfterEach
void tearDown(SessionFactoryScope scope) {
scope.inTransaction( session -> {
session.createQuery( "delete from Account" ).executeUpdate();
} );
scope.getSessionFactory().getSchemaManager().truncate();
}

@Entity(name = "Account")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void testFormulaWithAlias(SessionFactoryScope scope) {

@AfterEach
void tearDown(SessionFactoryScope scope) {
scope.inTransaction( session -> session.createQuery( "delete from Customer" ).executeUpdate() );
scope.getSessionFactory().getSchemaManager().truncate();
}

@Entity(name = "Customer")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void testFormulaAnnotationWithPartitionBy(SessionFactoryScope scope) {

@AfterEach
void tearDown(SessionFactoryScope scope) {
scope.inTransaction( session -> session.createQuery( "delete from DisplayItem" ).executeUpdate() );
scope.getSessionFactory().getSchemaManager().truncate();
}

@Entity(name = "DisplayItem")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void testLoading(SessionFactoryScope scope) {

@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> session.createQuery( "delete NonAuditedEntity" ).executeUpdate() );
scope.getSessionFactory().getSchemaManager().truncate();
}

@Entity( name = "NonAuditedEntity" )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void testGenerations(SessionFactoryScope scope) {

@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> session.createQuery( "delete AuditedEntity" ).executeUpdate() );
scope.getSessionFactory().getSchemaManager().truncate();
}

@Entity( name = "AuditedEntity" )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void testGenerationWithIdentityInsert(SessionFactoryScope scope) {

@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( (s) -> s.createQuery( "delete TheEntity" ).executeUpdate() );
scope.getSessionFactory().getSchemaManager().truncate();
}

@Entity( name = "TheEntity" )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public void testUpdateTimestampGeneration(SessionFactoryScope scope) {

@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( (s) -> s.createQuery( "delete TheEntity" ).executeUpdate() );
scope.getSessionFactory().getSchemaManager().truncate();
}

@Entity( name = "TheEntity" )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void testGenerations(SessionFactoryScope scope) {

@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> session.createQuery( "delete AuditedEntity" ).executeUpdate() );
scope.getSessionFactory().getSchemaManager().truncate();
}

@Entity( name = "AuditedEntity" )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void testGenerations(SessionFactoryScope scope) {

@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> session.createQuery( "delete AuditedEntity" ).executeUpdate() );
scope.getSessionFactory().getSchemaManager().truncate();
}

@Entity( name = "AuditedEntity" )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void testGenerations(SessionFactoryScope scope) {

@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> session.createQuery( "delete AuditedEntity" ).executeUpdate() );
scope.getSessionFactory().getSchemaManager().truncate();
}

@Entity( name = "AuditedEntity" )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void testGenerations(SessionFactoryScope scope) {

@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> session.createQuery( "delete AuditedEntity" ).executeUpdate() );
scope.getSessionFactory().getSchemaManager().truncate();
}

@Entity( name = "AuditedEntity" )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void testGenerations(SessionFactoryScope scope) {

@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> session.createQuery( "delete AuditedEntity" ).executeUpdate() );
scope.getSessionFactory().getSchemaManager().truncate();
}

@Entity( name = "AuditedEntity" )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void testLoading(SessionFactoryScope scope) {

@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> session.createQuery( "delete NonAuditedEntity" ).executeUpdate() );
scope.getSessionFactory().getSchemaManager().truncate();
}

@Entity( name = "NonAuditedEntity" )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void test(SessionFactoryScope scope) {

@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( session -> session.createQuery( "delete WithGeneratedAlways" ).executeUpdate() );
scope.getSessionFactory().getSchemaManager().truncate();
}

@Entity(name="WithGeneratedAlways")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void test(SessionFactoryScope scope) {

@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( session -> session.createQuery( "delete WithDefault" ).executeUpdate() );
scope.getSessionFactory().getSchemaManager().truncate();
}

@Entity(name="WithDefault")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void test(SessionFactoryScope scope) {

@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( session -> session.createQuery( "delete WithDefault" ).executeUpdate() );
scope.getSessionFactory().getSchemaManager().truncate();
}

@Entity(name="WithDefault")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void test(SessionFactoryScope scope) {

@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( session -> session.createQuery( "delete WithDefault" ).executeUpdate() );
scope.getSessionFactory().getSchemaManager().truncate();
}

@Entity(name="WithDefault")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void test(SessionFactoryScope scope) {

@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( session -> session.createQuery( "delete WithDefault" ).executeUpdate() );
scope.getSessionFactory().getSchemaManager().truncate();
}

@Entity(name="WithDefault")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void testWithExplicit(SessionFactoryScope scope) {

@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( session -> session.createQuery( "delete WithDefault" ).executeUpdate() );
scope.getSessionFactory().getSchemaManager().truncate();
}

@Entity(name="WithDefault")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void test(SessionFactoryScope scope) {

@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( session -> session.createQuery( "delete WithDefault" ).executeUpdate() );
scope.getSessionFactory().getSchemaManager().truncate();
}

@Entity(name="WithDefault")
Expand Down
Loading
Loading