0

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 parentattribute, 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:

  • Possible duplicate of [Spring - No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call](https://stackoverflow.com/questions/32269192/spring-no-entitymanager-with-actual-transaction-available-for-current-thread) – XtremeBaumer Sep 04 '19 at 10:06
  • edited my question, this not solved my problem – Raphael G. Sep 04 '19 at 10:22
  • I'd say that error speaks for itself. Think about how the FK is set up. Also always post the exact setup you are using and not some old version – XtremeBaumer Sep 04 '19 at 10:34
  • Have you tried cascading on the DB level? Alos which DB do you use? – Konstantin Triger Sep 05 '19 at 22:41

0 Answers0