简体   繁体   中英

Java Spring boot hibernate delete cascade data

I've a problem with right removing data from db. I use Hibernate orm in my spring boot app and now is the moment when I want to delete user from db. But there're realations and some tables contain foreign keys to parent table user. How to delete all the data linked where are foreign keys? Here are my all tables: and name of the columns with keys:

- User - id
- Workers - id(fk)
- Resetkeys - userId(fk)
- UserRole - userId(fk)
- Tokens - userId(fk)

And how delete user with all this data? Thanks for help!

Did you use relation annotations in your model? Like this,

@OneToMany(cascade = CascadeType.REMOVE )

Refer to the spec https://docs.oracle.com/cd/E19798-01/821-1841/bnbqm/index.html

( Edited )

Without any reference to frameworks like Spring or Hibernate, you can simply set up your database tables with constraints on the foreign key columns in order to delete all related records when the "user" record is deleted. The constraint is referred to as ON DELETE CASCADE , and if you define it on the foreign key column of a joined table, the RDBMS will automatically delete all the records for which the foreign key matches the selected user ID of the deleted record.

Try doing some research on the ON DELETE CASCADE constraint specifically for the database server you are using for more details.

( Edit #2 ) Here is a modified example from the MySQL documentation :

CREATE TABLE product (
  category INT NOT NULL, id INT NOT NULL,
  price DECIMAL,
  PRIMARY KEY(category, id)
)   ENGINE=INNODB;

CREATE TABLE customer (
  id INT NOT NULL,
  PRIMARY KEY (id)
)   ENGINE=INNODB;

CREATE TABLE product_order (
  no INT NOT NULL AUTO_INCREMENT,
  product_category INT NOT NULL,
  product_id INT NOT NULL,
  customer_id INT NOT NULL,

  PRIMARY KEY(no),
  INDEX (product_category, product_id),
  INDEX (customer_id),

  FOREIGN KEY (product_category, product_id)
    REFERENCES product(category, id)
    ON DELETE CASCADE,

  FOREIGN KEY (customer_id)
    REFERENCES customer(id)
)   ENGINE=INNODB;

The example shows a relationship between product and product_order . When deleting a record from the product table (just simply running DELETE FROM product WHERE id=... and category=... ), all the referenced records in the product_order table will be automatically deleted without any additional SQL statements. On the other hand, when deleting a row from the customer table you will get the same error you are getting now, because the default constraint restricts delete operations on records in that case.

For example ;

 DELETE FROM progress FROM progress INNER JOIN offices ON progress.RegNo = offices.RegNo WHERE offices.ProjectID = :id;
    DELETE FROM offices WHERE offices.ProjectID = :id;
    DELETE FROM workrooms WHERE workrooms.ProjectID = :id;

NB: Use parameters to pass values ​​to the query instead of adding the values ​​directly to the string. If you don't, you can expose your code to SQL Injection. So you have to pay some attention.I wanted to explain the offices and rooms here by taking an example.

what about disabling and re-enabling the foreign key constraints before and after dlete operation.

alter table Resetkeys nocheck constraint all
delete from Resetkeys ...
alter table Resetkeys check constraint all

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM