【发布时间】:2023-02-25 12:46:53
【问题描述】:
有两个表:
User table
+----+----------------------------+-------------+
| id | date_created_user | email |
+----+----------------------------+-------------+
| 7 | 2023-02-23 13:23:09.085897 | www@www.www |
| 16 | 2023-02-25 14:23:31.691560 | qqq@qqq.qqq |
| 17 | 2023-02-25 14:24:02.089010 | aaa@aaa.aaa |
| 18 | 2023-02-25 14:24:24.708500 | xxx@xxx.xxx |
| 19 | 2023-02-25 14:25:19.253770 | ooo@ooo.ooo |
+----+----------------------------+-------------+
Deletion table
+----+----------------+----------------------------+---------+
| id | active | date | user_id |
+----+----------------+----------------------------+---------+
| 10 | false | 2023-02-25 14:23:31.691560 | 16 |
| 11 | false | 2023-02-25 14:24:02.089010 | 17 |
| 12 | true | 2023-02-25 14:24:24.708500 | 18 |
| 13 | true | 2023-02-25 14:25:19.253770 | 19 |
+----+----------------+----------------------------+---------+
有必要从这些表中删除某些记录。
条件如下:
如果在Deletiontable,active字段包含值false并且已经超过 24 小时date字段,则需要删除这条记录,并从Usertable。
这user_id键入Deletiontable。
我的仓库
@Transactional(readOnly = true)
@Repository
public interface DeletionRepository extends JpaRepository<Deletion, Long> {
@Transactional
@Modifying
@Query("DELETE FROM Deletion as a WHERE a.active = false AND a.date <= :date")
void deleteDeletionByActiveAndDate(@Param("date") String date);
}
【问题讨论】: