spring - 为什么 JpaRepository.delete() 在删除实体之前检索它?
问题描述
当我使用它的 id 删除一个实体时,我得到以下日志输出。
2020-09-19 17:07:35.536 DEBUG 14544 --- [nio-8080-exec-2] org.hibernate.SQL : select comment0_.id as id1_15_0_, comment0_.acceptedAsAnswer as accepted2_15_0_, comment0_.commentDate as commentD3_15_0_, comment0_.commenter_id as comment10_15_0_, comment0_.commenterIp as commente4_15_0_, comment0_.content as content5_15_0_, comment0_.dateDeleted as dateDele6_15_0_, comment0_.deleted as deleted7_15_0_, comment0_.deleter_id as deleter11_15_0_, comment0_.journal_entry_id as journal12_15_0_, comment0_.language_id as languag13_15_0_, comment0_.lastEditedOn as lastEdit8_15_0_, comment0_.likeTotal as likeTota9_15_0_, comment0_.owning_comment_id as owning_14_15_0_ from comments comment0_ where comment0_.id=?
2020-09-19 17:07:35.562 DEBUG 14544 --- [nio-8080-exec-2] org.hibernate.SQL : delete from comments where id=?
我正在构建一个使用 Amazon 数据库服务的 Web 应用程序,因此该应用程序发出的每个请求都需要额外付费。为什么 spring 在删除之前先检索对象,有什么方法可以阻止它这样做?
解决方案
正如@Naim 指出的那样,休眠首先检索对象,因为可能存在拦截器。JpaRepository
这是我使用实现存储库直接对数据库进行删除查询的解决方案。
@Modifying
@Transactional
@Query(nativeQuery = true, value = "DELETE FROM comments WHERE id = :id")
void deleteById(@Param("id") Long id);
自定义方法deleteById
不需要先获取实体。
推荐阅读
- javascript - 在最接近点击事件的线上添加点到 SVG 多边形
- javascript - 找到数组中最短的单词
- python-3.x - Pandas 通过正则表达式选择列并通过 if 更改其值,否则
- javascript - Discord.js Bots // 欢迎功能不起作用
- flutter - 将函数作为参数传递 Flutter - 无法在初始化程序中访问实例成员“_handleChange”
- swift - Swift Subtracting Double 给出了奇怪的结果
- python - 每次我重新启动笔记本时,pandas value_count() 都会显示不同的值
- clojure - 如何使用记录参数检测函数?
- html - 我的 HTML 和 CSS 的结构是否正确?
- python - 加快 scipy 最小化