Hibernate在一个事务中删除和插入

huangapple go评论82阅读模式
英文:

Hibernate delete and insert in one transaction

问题

这是我的JPA存储库:

interface ClientApplicationPriorityRepository : JpaRepository<ClientApplicationPriority, Long> {

    fun deleteAllByClientId(clientId: String): Int
}

这是我需要首先删除,然后插入的方法(优先级很重要):

@Transactional
public void saveClientApplicationPriorities(String clientId, Boolean isFoldersPresented, List<ClientApplicationPriority> clientApplicationPriorities) {
    clientApplicationPriorityRepo.deleteAllByClientId(clientId);
    clientApplicationPriorityRepo.flush();
    if (isFoldersPresented) {
        clientApplicationPriorityRepo.saveAll(clientApplicationPriorities);
    }
}

我必须在一个事务中执行,但是在测试方法之后,我看到数据库中什么都没有被删除。问题在哪里,如何解决?

英文:

Here is my JPA repository:

interface ClientApplicationPriorityRepository : JpaRepository&lt;ClientApplicationPriority, Long&gt; {

    fun deleteAllByClientId(clientId: String): Int
}

Here is my method where I need first to delete and second to insert (the priority is important):

@Transactional
    public void saveClientApplicationPriorities(String clientId, Boolean isFoldersPresented, List&lt;ClientApplicationPriority&gt; clientApplicationPriorities) {
        clientApplicationPriorityRepo.deleteAllByClientId(clientId);
        clientApplicationPriorityRepo.flush();
        if (isFoldersPresented) {
            clientApplicationPriorityRepo.saveAll(clientApplicationPriorities);
        }
    }

I have to do it in a transaction, but after the method in tests I see that nothing was deleted from the DB. What's the problem and how to solve it?

答案1

得分: 1

你可能需要使用以下方式注释您的 deleteAllByClientId() 函数:

    @Modifying
    @Transactional
英文:

You probably need to annotate your deleteAllByClientId() function with the followings:

    @Modifying
    @Transactional

huangapple
  • 本文由 发表于 2020年8月12日 17:32:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63373729.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定