My application can store folders and files, every folder can have some subfolders and got an parent_id
attribute pointing to his parent folder. The Code for this JPA-Object is shown below, also a screenshot from the database table.
Now I want to add a delete feature that deletes a Folder with all contents.
For example delete SubFolder-3
should delete SubFolder4, -5, -6 and itself.
I tried it with Cascade.Type=REMOVE on the parent
attribute, and added @transactional
to my delete method in repository. But i get this error:
Caused by: org.postgresql.util.PSQLException: ERROR: update or delete on table "cloud_folder" violates foreign key constraint "fk715hn6f0nslmk97oftbbtm0ab" on table "cloud_folder"
Detail: Key (id)=(5) is still referenced from table "cloud_folder".
Folder Object:
@Entity
public class CloudFolder {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
@ManyToOne
private CloudFolder parent;
// Getter and Setter
}
Folder Repository:
public interface CloudFolderRepository extends JpaRepository<CloudFolder, Integer> {
CloudFolder findByParentId(Integer id);
@Transactional
void deleteByParentId(Integer id);
}
Picture of my Folder Table here: