I have a User. That User has a Document. The Document has several Entries. If I delete a Document, then I would like to keep the children Entries, which will be orphans, for debugging purpose. Using the classes described at the bottom, when I delete a Document, I get a foreign key error:
Cannot delete or update a parent row: a foreign key constraint fails (
relation
.entry
, CONSTRAINTFK4001852ED0B1AFE
FOREIGN KEY (document_id
) REFERENCESdocument
(id
))
I am using a framework that creates the tables based on the models. Using cascade = CascadeType.REMOVE
makes it work but I do not want to remove the children. Is there a special CascadeType
I should be using?
User
@Entity
public class User {
public String name;
@OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
public Document document;
}
Document
@Entity
public class Document {
public String title;
@OneToOne
public User user;
@OneToMany(mappedBy = "document")
public List<Entry> entries;
}
Entry
@Entity
public class Entry {
public String data;
@ManyToOne
public Document document;
}