I'm using JPA in combination with Spring Boot (if it matters) and created two tables FileSystem and Media. Both are connected over an InheritanceType.JOINED relationship (see ER-diagram). Besides, Filesystem is a self-referencing table which stores a "tree" of files/folders with their childs and parents.
The code is as follows:
FileSystem.java
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class FileSystem {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column
private long id;
private String name;
public enum Type { FOLDER, FILE}
@ManyToOne
@JoinColumn(name="parent")
@JsonBackReference
private FileSystem parent;
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL,orphanRemoval = true)
@JsonManagedReference
List<FileSystem> childs = new ArrayList<FileSystem>();
FileSystem.Type type;
}
Media.java
@Entity
public class Media extends FileSystem implements FieldHandled{
@JsonIgnore
private FieldHandler fieldHandler;
@JsonIgnore
@Lob
@Column( length = 100000 )
@Basic(fetch = FetchType.LAZY, optional = false)
private byte[] data;
}
MediaRepository.java
public interface MediaRepository extends JpaRepository<Media, Long>{
Media save(Media pageTemplate);
Media findOne(Long id);
public List<Media> findAllByParentId(Long parent);
public List<Media> findAllByParentIsNull();
public List<Media> findByName(String name);
}
Deletion in my controller method
...
MediaRepository mediaRepository;
mediaRepository.delete(mediaToDelete);
...
Adding new media's, fetching them etc. works fine so far. But when I call delete()
for a FileSystem
that has multiple childs (and subchilds) using my extended JpaRepository
I get the following exception:
Cannot delete or update a parent row: a foreign key constraint fails (
repo
.file_system
, CONSTRAINTFKf57w77uiwcpfmbtl9lpuq9j9m
FOREIGN KEY (parent
) REFERENCESfile_system
(id
))
The strangest thing about this is the log. It seems that in the first step JPA is removing the media row (which IMO is correct since the InheritanceType is JOINED) and then tries to remove the filesystem row (which IMO is not correct because it should first remove all it's childs recursively).:
2016-10-19 16:21:43.530 TRACE 11412 --- [nio-8080-exec-1] org.hibernate.type.EnumType : Returning [FOLDER] as column [type3_6_0_]
2016-10-19 16:21:43.530 DEBUG 11412 --- [nio-8080-exec-1] org.hibernate.SQL : delete from media where id=?
Hibernate: delete from media where id=?
2016-10-19 16:21:43.531 TRACE 11412 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [BIGINT] - [65]
2016-10-19 16:21:43.533 DEBUG 11412 --- [nio-8080-exec-1] org.hibernate.SQL : delete from file_system where id=?
Hibernate: delete from file_system where id=?
2016-10-19 16:21:43.534 TRACE 11412 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [BIGINT] - [65]
2016-10-19 16:21:43.535 WARN 11412 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1451, SQLState: 23000
2016-10-19 16:21:43.535 ERROR 11412 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : Cannot delete or update a parent row: a foreign key constraint fails