Spring CrudRepository的deleteAll()方法不起作用。

10

在尝试使用Spring的CrudRepository删除数据库中所有记录时,测试Controller类时似乎什么也没发生。默认情况下,它似乎不会刷新。

我从未在浏览器中通过实际控制器调用尝试过这个存储库,只是使用Junit测试,但我认为它应该工作正常! :)

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@ActiveProfiles("test")
@Transactional
public class CostumerControllerIntegrationTest {

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private CostumerService costumerService;

    private Costumer costumer;

    @Before
    public void before() {
        this.costumer = this.exampleBuilder();
        costumerService.saveAll(this.costumer);
    }

    @After
    public void after() {
        costumerService.deleteAll();
    }

    @Test
    public void Should_ReturnStandardError_When_NotFoundById() throws Exception {
        //implementation
    }


private Costumer exampleBuilder() {

    Costumer costumer = new Costumer("Test", "Test", "Test", CostumerType.LEGAL_PERSON);
    State state = new State("Example State");
    City city = new City("Example Sity", state);
    Address address = new Address("Example Address",
            "Example Address", "Example Address",
            "Example Address", city, costumer);

    costumer.getAddresses().add(address);

    return costumer;
}
@Service
@Transactional
public class CostumerService {

    @Autowired
    private CostumerRepository repository;

    public void deleteAll() {
        repository.deleteAll();
    }

   //another methods

}

扩展CrudRepository的代码库

@Repository
public interface CostumerRepository extends CrudRepository<Costumer, Integer> {

}

根据@TheCoder的评论,启用显示SQL hibernate.show_sql=true后,结果如下:

@After上的deleteAll()

@After
public void after() {
    costumerService.deleteAll();
}

输出的 SQL 语句:

2019-02-27 06:07:06 - HHH000397: Using ASTQueryTranslatorFactory
Hibernate: 
    select
        costumer0_.id as id1_3_,
        costumer0_.cpf_cnpj as cpf_cnpj2_3_,
        costumer0_.email as email3_3_,
        costumer0_.name as name4_3_,
        costumer0_.type as type5_3_ 
    from
        costumers costumer0_

@AfterTransaction 中的 deleteAll() 方法会在输出的 SQL 语句中包含删除查询。

@AfterTransaction
    public void afterTransatcion(){
        // List<Costumer> costumers = costumerService.findAll();
        costumerService.deleteAll();
    }


Hibernate: 
    select
        costumer0_.id as id1_3_,
        costumer0_.cpf_cnpj as cpf_cnpj2_3_,
        costumer0_.email as email3_3_,
        costumer0_.name as name4_3_,
        costumer0_.type as type5_3_ 
    from
        costumers costumer0_
Hibernate: 
    delete 
    from
        phones 
    where
        costumer_id=?
Hibernate: 
    delete 
    from
        costumers 
    where
        id=?

你的测试连接到了真实数据库还是内存数据库? - Adina Rolea
在你的测试中尝试使用deleteAll()。将hibernate.show_sql=true设置为true。这将确保我们知道删除SQL是否被触发。 - The Coder
@TheCoder 这就是问题所在!由于某种原因,测试没有自动回滚。测试结束后,我可以在数据库中看到记录。我只需使用 hibernate.show_sql=true 发送结果即可。 - Pablo Souza
1
你的数据库类型是InnoDB吗? 这个链接可能会有帮助 https://dev59.com/W03Sa4cB1Zd3GeqPx8Wj - The Coder
1
不用谢。 - The Coder
显示剩余6条评论
2个回答

3
在我的JUnit中,在@BeforeEach方法中添加repository.deleteAllInBatch()对我有用。只在运行JUnit时遇到了此问题。
@BeforeEach
public void config()
{
   repository.deleteAllInBatch()
}

0

当事务关闭时,数据将从数据库中删除。这应该只发生在测试方法的最后阶段。

但是,因为您正在使用事务性测试,所有事务都将在测试后自动回滚。

默认情况下,测试事务将在测试完成后自动回滚;但是,可以通过@Commit和@Rollback注释进行声明性地配置事务提交和回滚行为。


但是即使调用deleteAll(),数据库中的记录也永远不会被删除。 - Pablo Souza
只有在测试中调用该记录时,删除操作才会在测试结束时回滚。请在您的测试中添加@Rollback(false)注解。 - Selindek
抱歉,无法与 @Rollback(false) 一起使用。也无法与 @Commit 一起使用。同时使用 @Rollback(false)@Commit 也不行!在 @After 测试调用 deleteAll() 后,数据仍然存在于数据库中。 - Pablo Souza
1
它可以与 @AfterTransaction public void afterTransatcion(){ costumerService.deleteAll(); } 一起使用,但考虑到默认的回滚行为,我不确定它是否有意义。 - Pablo Souza

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接