For some reason my delete is not cascading when I try to delete the parent element which has an elementcollection in it, the two classes are as follows:
@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;
And:
@Embeddable
@JsonIgnoreProperties(ignoreUnknown = true)
public class Product{
@Embedded
private Status quick_status;
Currently this is the only field that I have in the class because I have removed all the others trying to figure out why when I try to delete a parent object the delete does not cascade to the Products table. Below is the query I am running:
DELETE FROM Timestamps list WHERE list.last_updated !=0;
The last_updated value will always be non-zero, so I am just using this query to test deleting, but even when I run the query in the mysql shell I get "Cannot delete or update a parent row: a foreign key constraint fails" I thought that the elementcollection annotation was suppose to automatically cascade, is there something that I am missing?
EDIT, when Below are the sql commands that Hibernate is sending, as you will notice on the third one it is missing the cascade.
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)
EDIT 2: Below is the @Query that I have for the ProductListRepository class, I included on the query that is relevant for deleting.
@Repository
public interface ProductListRepository extends CrudRepository<ProductList, Integer>{
@Modifying
@Query(value = "DELETE FROM Timestamps list WHERE list.last_updated !=0", nativeQuery = true)
void deleteOld();
}