为什么从@springboottest调用时,jparepository不遵守数据库?

发布于 2025-01-22 13:52:51 字数 1417 浏览 0 评论 0原文

当repository.save(t)从我的服务中调用(又是从我的控制器调用)时,所有这些都可以正常工作,并且对象插入数据库表中;但是,当从我的测试类调用服务时,Hibernate会返回创建的对象,但并未真正将事务刷到数据库中。我尝试在测试类中以及@Test方法中使用@transactinal和@commit,但结果没有差异。我还尝试了其他解决方案,其中涉及使用org.springframework.test.context.transaction.testtransaction类,但是任何方法都会呼吁此类引发异常。 这是我的超级课程进行测试:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public abstract class QaApplicationTest {
    protected abstract void initializeTest() throws Exception;

    protected abstract void cleanupTestEffects() throws Exception;
}

这是我的具体测试类:

public class RequestControllerTest extends QaApplicationTest {
    @Autowired
    private SiteService siteService;

    @Autowired
    private RequestService requestService;

    @Test
    @Transactional
    public void givenObject_whenInsertToDB_thenCreated() throws Exception{
        Site siteObject = siteService.save(siteObject); //Here I need a commit.

        Request request = new Request(site.getId());
        Request savedRequest = requestService.save(request); //Here database returns "Parent Key Not Found" error.

        Assertions.assertTrue(savedRequest.getId()>0);
        
    }
}

我知道测试方法上的 @transactional用于回滚方法内部所做的所有更改,但是,在我的情况下,更改甚至都不是在第一位。我已经使用了 @org.springframework.transaction.annotation.transactional,这是正确的注释。我不知道我做错了哪一部分!有什么想法吗?

When repository.save(t) is called from my service, which is in turn called from my controller, all works just fine, and the object is inserted into the database table; But, when the service is called from my test class, Hibernate returns the created object but does not really flush the transaction into the database. I have tried using @Transactinal and @Commit in my test class and also on my @Test methods, but no difference in the result. I have also tried other solutions which involve using org.springframework.test.context.transaction.TestTransaction class, but any method call on this class throws an exception.
this is my super class for test:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public abstract class QaApplicationTest {
    protected abstract void initializeTest() throws Exception;

    protected abstract void cleanupTestEffects() throws Exception;
}

And this is my concrete test class:

public class RequestControllerTest extends QaApplicationTest {
    @Autowired
    private SiteService siteService;

    @Autowired
    private RequestService requestService;

    @Test
    @Transactional
    public void givenObject_whenInsertToDB_thenCreated() throws Exception{
        Site siteObject = siteService.save(siteObject); //Here I need a commit.

        Request request = new Request(site.getId());
        Request savedRequest = requestService.save(request); //Here database returns "Parent Key Not Found" error.

        Assertions.assertTrue(savedRequest.getId()>0);
        
    }
}

I know the @Transactional on test methods are used to roll back all the changes made inside the method, however, In my case, the changes are not even committed in the first place. And I have used @org.springframework.transaction.annotation.Transactional which is the correct annotation. I don't know which part I am doing wrong! Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他夏了夏天 2025-01-29 13:52:51

我的同事发现了这个问题。我们使用了一个在休拜酸盐上启用了批处理的第三方库(Camunda)。因此,通过禁用批处理操作,该问题已解决,并且插入实际上正在发生。不确定,为什么我们只在春季测试中面对这一点而不是在主要应用程序中面对。如果有人发表评论,我们将不胜感激。

My colleague found the issue; we had used a third-party library (Camunda) that had enabled batch-insert on Hibernate. So by disabling the batch operation the issue was resolved and the insert is actually taking place now. Not sure, why we faced this only in Spring Test and not in the main application though. if anyone has a comment, we appreciate it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文