使用 Spring Data JPA 我在同一事务内有下一个流程(REQUIRES_NEW):
-
使用此 Spring Data JPA 存储库方法删除一组用户的预测。
@Query(value = "DELETE FROM TRespuestaUsuarioPrediccion resp WHERE resp.idEvento.id = :eventId AND resp.idUsuario.id = :userId")
@Modifying
void deleteUserPredictions(@Param("userId") int userId, @Param("eventId") int eventId);
-
插入新用户的预测并保存主对象(事件)。
eventRepository.save(event);
当此服务完成时,提交由 AOP 进行,但仅在第一次尝试中有效...不适用于下一次尝试...
如何在不迭代事件的预测条目并更新其中的每个预测条目的情况下管理这种情况?
UPDATE
我尝试过,但它不起作用(适配器插入我之前删除的对象):
@Transactional(propagation=Propagation.REQUIRES_NEW, rollbackFor=PlayTheGuruException.class)
private void updateUserPredictions(final TUsuario user, final TEvento event, final SubmitParticipationRequestDTO eventParticipationRequestDTO)
{
eventRepository.deleteUserPredictions(user.getId(), event.getId());
EventAdapter.predictionParticipationDto2Model(user, event, eventParticipationRequestDTO);
eventRepository.save(event);
}
Hibernate 改变了命令的顺序。它按以下顺序工作:
以特殊顺序执行所有 SQL 和二级缓存更新,以便不会违反外键约束:
1. Inserts, in the order they were performed
2. Updates
3. Deletion of collection elements
4. Insertion of collection elements
5. Deletes, in the order they were performed
事实确实如此。当刷新时,Hibernate 在删除语句之前执行所有插入。
可能的选项是:
1. 在删除之后显式调用entityManager.flush()。
或者
2. 尽可能更新现有行并从其余行创建 ToBeDeleted 列表。这将确保现有记录更新为新值并保存全新记录。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)