I'm using JPA / Hibernate.
Let's say I have folders and files. Every folder can contain more folders and files. Every file knows what its root folder is (not the parent).
@Entity
public class Folder{
...
@OneToMany
@JoinColumn("folder_id")
private List<Folder> folders;
@OneToMany
@JoinColumn("file_id")
private List<File> files;
...
}
@Entity
public class File {
...
@ManyToOne
private Folder rootFolder;
...
}
I create a new file and a new folder. I put the file in the folder. Now the file is in the folders "files"-collection and the folder is referenced in the files "rootFolder"-variable.
If I persist this, I get the "object references an unsaved transient instance" or "save the transient instance before flushing: entities.file.rootFolder -> entities.Category" (depends on what gets persisted first).
There hast to be a way I can annotate this, so it will work not matter what I save first!? I could work around it somehow programmatically to insert first one Entity without referencing the other, then insert the other and then put in the reference, but I don't think that that should be neccessary here.
I searched for this, but all I could find was @OneToMany combined with @ManyToOne, but in my case I can't use it. A folder shouldn't know if it is a root folder.
I'd appreciate any thoughts or directions to tutorials.