There are two tables:
User table `user`
+----+----------------------------+-------------+
| id | date_created_user | email |
+----+----------------------------+-------------+
| 7 | 2023-02-23 13:23:09.085897 | [email protected] |
| 16 | 2023-02-25 14:23:31.691560 | [email protected] |
| 17 | 2023-02-25 14:24:02.089010 | [email protected] |
| 18 | 2023-02-25 14:24:24.708500 | [email protected] |
| 19 | 2023-02-25 14:25:19.253770 | [email protected] |
+----+----------------------------+-------------+
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 | |
+---------------+--------+------+-----+---------+-------+
It is necessary to delete certain records from these tables.
The condition is the following:
If in the Deletion
table
, the active
field contains the value false
and more than 24 hours have passed in the date
field, then you need to delete this record and delete the record from the User
table
.
The user_id
key in the Deletion
table
.
And also, there is a table of relations user_role
. This is a table of the user and his role. It will not be possible to delete a user if there is a relationship. Therefore, it is also necessary to remove this relation.
My repository
@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);
}
2
Answers
Given that you need to do some datetime manipulations in your delete query, which tend to be highly database specific, I suggest using the following native MySQL query:
If you also want to delete the record from the
Deleted
table, then setup a cascading foreign key onuser_id
, such that deleting the parent record from theUser
table will also delete the dependent record.If you can’t setup cascading deletion, on MySQL you could also use the following delete join:
NOTE: I wrote this answer partially with the assistance of GitHub Copilot.
TL;DR: Use an appropriate
CascadeType
such asREMOVE
orALL
on yourDeletion
entity.If you define the
User
andDeletion
entities as follows:And use the following
persistence.xml
file:And use this schema (generated by Hibernate automatically):
Then this demo code shows how the cascading effect works:
Demo video: https://www.youtube.com/watch?v=PCZcJdi9wf8.
Sample code: https://github.com/behrangsa/so-75563217
For the age comparison, you can add the date/time logic to your JPQL query.