简体   繁体   English

Java Spring boot hibernate 删除级联数据

[英]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.我在 Spring Boot 应用程序中使用了 Hibernate orm,现在是我想从数据库中删除用户的时刻。 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 ) @OneToMany(cascade = CascadeType.REMOVE )

Refer to the spec https://docs.oracle.com/cd/E19798-01/821-1841/bnbqm/index.html请参阅规范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.无需参考 Spring 或 Hibernate 等框架,您可以简单地设置数据库表,并对外键列进行约束,以便在删除“用户”记录时删除所有相关记录。 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.该约束称为ON DELETE CASCADE ,如果您在连接表的外键列上定义它,RDBMS 将自动删除外键与已删除记录的选定用户 ID 匹配的所有记录。

Try doing some research on the ON DELETE CASCADE constraint specifically for the database server you are using for more details.尝试针对您正在使用的数据库服务器的ON DELETE CASCADE约束进行一些研究,以了解更多详细信息。

( Edit #2 ) Here is a modified example from the MySQL documentation : 编辑 #2 )这是MySQL 文档中修改示例:

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 .该示例显示了productproduct_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.当从product表中删除一条记录时(只需运行DELETE FROM product WHERE id=... and category=... ), product_order表中所有引用的记录将被自动删除,无需任何额外的 SQL 语句。 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.另一方面,当从customer表中删除一行时,您将得到与现在相同的错误,因为在这种情况下默认约束限制对记录的删除操作。

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.如果不这样做,您可以将代码暴露给 SQL 注入。 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.在 dlete 操作之前和之后禁用和重新启用外键约束怎么样。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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