Java Spring级联元素集合删除

Jok*_*313 7 mysql spring cascade cascading-deletes spring-boot

出于某种原因,当我尝试删除包含 elementcollection 的父元素时,我的删除不是级联的,这两个类如下:

@Entity
@Table(name="Timestamps")
@JsonIgnoreProperties(ignoreUnknown = true)
public class ProductList {
    private boolean success;
    @Id
    private Date lastUpdated;

    private String time = "minute";

    @ElementCollection
    @MapKeyColumn(name="product_id")
    @CollectionTable(name="Products")
    private Map<String,Product> products;
Run Code Online (Sandbox Code Playgroud)

和:

@Embeddable
@JsonIgnoreProperties(ignoreUnknown = true)
public class Product{
    @Embedded
    private Status quick_status;
Run Code Online (Sandbox Code Playgroud)

目前这是我在班级中唯一的字段,因为我已经删除了所有其他字段,试图找出为什么当我尝试删除父对象时删除不会级联到 Products 表。以下是我正在运行的查询:

DELETE FROM Timestamps list WHERE list.last_updated !=0;
Run Code Online (Sandbox Code Playgroud)

last_updated 值将始终为非零,因此我只是使用此查询来测试删除,但即使我在 mysql shell 中运行查询时,我也会收到“无法删除或更新父行:外键约束失败”我认为 elementcollection 注释应该自动级联,有什么我遗漏的吗?

编辑,当下面是 Hibernate 发送的 sql 命令时,你会注意到第三个它缺少级联。

Hibernate: create table products (product_list_last_updated datetime(6) not null, buy_price float not null, sell_price float not null, product_id varchar(255) not null, primary key (product_list_last_updated, product_id)) engine=InnoDB
Hibernate: create table timestamps (last_updated datetime(6) not null, success bit not null, time varchar(255), primary key (last_updated)) engine=InnoDB
Hibernate: alter table products add constraint FKo31ur4gpvx1d5720rgs3qaawi foreign key (product_list_last_updated) references timestamps (last_updated)
Run Code Online (Sandbox Code Playgroud)

编辑 2:下面是 ProductListRepository 类的 @Query,我包含在与删除相关的查询中。

@Repository
public interface ProductListRepository extends CrudRepository<ProductList, Integer>{
    @Modifying
    @Query(value = "DELETE FROM Timestamps list WHERE list.last_updated !=0", nativeQuery = true)
    void deleteOld();
}
Run Code Online (Sandbox Code Playgroud)

Ric*_*mes 0

(警告:我不知道设计师的想法。)

对我来说,自动删除数据是可怕的。我的一个轻微的误解可能会导致数据被删除,这是我没有预料到的。

当然,在精心设计的教科书式模式中,数据的结构将是完美的,并且应该很明显要删除什么和不应该删除什么。

但教科书模式仅限于教科书。