英文:
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<ClientApplicationPriority, Long> {
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<ClientApplicationPriority> 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论