# The best way to clean up test data with Spring and Hibernate

DevFeed: [The best way to clean up test data with Spring and Hibernate](<https://devfeed.tech/articles/the-best-way-to-clean-up-test-data-with-spring-and-hibernate-21982.md>)

Original publisher: [Read original article](<https://vladmihalcea.com/clean-up-test-data-spring/>)

Author: vladmihalcea

Published: 2025-11-11T08:05:24Z

Content type: article

Language: en

Sources: [Vlad Mihalcea](<https://devfeed.tech/sources/vlad-mihalcea.md>)

Topics: [Hibernate](<https://devfeed.tech/topics/hibernate.md>), [Software Testing](<https://devfeed.tech/topics/software-testing.md>), [Spring Framework](<https://devfeed.tech/topics/spring-framework.md>), [Spring Boot](<https://devfeed.tech/topics/spring-boot.md>), [test data](<https://devfeed.tech/topics/test-data.md>), [Test automation](<https://devfeed.tech/topics/test-automation.md>)

Tags: [batching](<https://devfeed.tech/tags/batching.md>), [best-practices](<https://devfeed.tech/tags/best-practices.md>), [cleanup](<https://devfeed.tech/tags/cleanup.md>), [hibernate](<https://devfeed.tech/tags/hibernate.md>), [integration-testing](<https://devfeed.tech/tags/integration-testing.md>), [jpa](<https://devfeed.tech/tags/jpa.md>), [logging](<https://devfeed.tech/tags/logging.md>), [schema](<https://devfeed.tech/tags/schema.md>), [spring](<https://devfeed.tech/tags/spring.md>), [spring-boot](<https://devfeed.tech/tags/spring-boot.md>), [spring-data-jpa](<https://devfeed.tech/tags/spring-data-jpa.md>), [test](<https://devfeed.tech/tags/test.md>), [test-data](<https://devfeed.tech/tags/test-data.md>), [testing](<https://devfeed.tech/tags/testing.md>), [truncatemappedobjects](<https://devfeed.tech/tags/truncatemappedobjects.md>)

## AI overview

The excerpt recommends avoiding `@DataJpaTest` when its default test behavior gets in the way of better SQL logging or realistic transaction handling. It says the annotation auto-configures JPA-related Spring beans, logs SQL to the console, and wraps each test in a transaction that is rolled back afterward. The author argues Spring's SQL logging property cannot show bind values or whether batching is used, and recommends DataSource-Proxy for richer logging and automatic detection of N+1 query issues during tests. The excerpt begins to explain problems with rollback-based test transactions but cuts off before giving the proposed cleanup approach, so it does not establish the article's full recommendation for cleaning test data.

## Source excerpt

Introduction In this article, I'm going to show you the best way to clean up test data when using Spring and Hibernate. While it's very common to use the @DataJpaTest annotation when implementing integration tests, in this blog post, you will see why you are better off avoiding using this annotation. Why I don't use @DataJpaTest As explained by the manual, Spring Boot provides the @DataJpaTest annotation for Spring Data JPA integration testing. Once the @DataJpaTest annotation is added to an integration test, Spring Boot will make the following changes to the... Read More The post The best way to clean up test data with Spring and Hibernate appeared first on Vlad Mihalcea.