有两个表:
User table `user`
+----+----------------------------+-------------+
| 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 `deletion`
+----+----------------+----------------------------+---------+
| 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 |
+----+----------------+----------------------------+---------+
Relationship table `user_role`
+---------+---------------+
| user_id | role_id |
+---------+---------------+
| 7 | 1 |
| 16 | 2 |
| 17 | 2 |
| 18 | 2 |
| 19 | 2 |
+---------+---------------+
DESCRIBE User `user`
+-------------------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------------+-------------+------+-----+---------+----------------+
| id | bigint | NO | PRI | NULL | auto_increment |
| email | varchar(58) | NO | UNI | NULL | |
| enabled | bit(1) | NO | | NULL | |
| password | varchar(65) | NO | | NULL | |
| token | varchar(45) | YES | UNI | NULL | |
+-------------------------+-------------+------+-----+---------+----------------+
DESCRIBE Deletion `deletion`
+--------------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+-------------+------+-----+---------+----------------+
| id | bigint | NO | PRI | NULL | auto_increment |
| active | bit(1) | NO | | NULL | |
| date | datetime(6) | NO | | NULL | |
| user_id | bigint | YES | MUL | NULL | |
+--------------------+-------------+------+-----+---------+----------------+
DESCRIBE `user_role`
+---------------+--------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------+------+-----+---------+-------+
| user_id | bigint | NO | PRI | NULL | |
| role_id | bigint | NO | PRI | NULL | |
+---------------+--------+------+-----+---------+-------+
有必要从这些表中删除某些记录。
具体情况如下:
如果在**Deletion
table
中,active
字段包含值false
,并且在date
字段中已超过24小时,则需要删除此记录,并从User
**table
中删除此记录。
Deletion
table
中的user_id
**键。
另外还有一个关系表**user_role
**,这是用户和角色的表,如果有关系的话就不能删除用户了,所以也需要去掉这个关系。
我的存储库
@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);
}
3条答案
按热度按时间zy1mlcev1#
考虑到您需要在删除查询中执行一些日期时间操作,而这些操作往往是高度特定于数据库的,我建议使用以下原生MySQL查询:
如果还想从
Deleted
表中删除记录,则在user_id
上设置一个级联外键,这样从User
表中删除父记录也会删除从属记录。如果你 * 不能 * 设置级联删除,在MySQL上你也可以使用下面的delete join:
hwazgwia2#
根据您提供的存储库接口,您似乎已经走上了正确的道路,可以使用JPQL查询根据给定条件从Deletion表中删除记录。但是,您还需要从User表中删除相应的记录。要完成此操作,您可以修改JPQL查询,以便也从Deletion表中检索要删除的记录的user_id。然后执行另一个查询以从User表中删除相应的记录。
下面是包含附加查询的存储库界面的修改版本:
在此修改版本中,findUserIdsToDelete方法从Deletion表中检索要删除的记录的user_id,并将其作为列表返回。deleteUserByIds方法获取此user_id列表,并从User表中删除相应的记录。
最后,deleteInactiveDeletationsAndRelatedUsers方法结合了上述方法,提供了一种更方便的方式来删除非活动删除及其相关用户。可以从服务层调用此方法,传入适当的日期值。注意,我将参数类型从String更改为Date,这是更适合日期值的类型。
nhaq1z213#
TL; DR:在
Deletion
实体上使用适当的CascadeType
,例如REMOVE
或ALL
。如果按如下方式定义
User
和Deletion
图元:并使用以下
persistence.xml
文件:并使用以下模式(由Hibernate自动生成):
下面的演示代码显示了级联效果的工作原理:
演示视频:https://www.youtube.com/watch?v=PCZcJdi9wf8。
样本代码:https://github.com/behrangsa/so-75563217
对于年龄比较,可以将日期/时间逻辑添加到JPQL查询中。