Hi @all,
my model contains a document and a file object. It is possible, that several documents point to one file, so it´s a manyToOne relation from document to file. Here the relevant parts from my document class:
Code:
@Entity
@org.hibernate.annotations.Proxy(lazy = true)
@Table(name = "`DOCUMENT`", schema = "MYSCHEME")
@org.hibernate.search.annotations.Indexed
@org.hibernate.search.annotations.Analyzer(impl = MyAnalyzer.class)
public class Document{
...
@ManyToOne(targetEntity = Fileh.class, fetch = FetchType.LAZY)
@JoinColumns({ @JoinColumn(name = "`FILEHID`", referencedColumnName = "`ID`") })
private File fileh;
...
}
The file has no idea of the existence of a document so there´s no link.
If I now create and save a document and a file, the following exception is thrown:
Code:
Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: File
Okay, the exception is clear, but due to my coding I thought I saved the file before:
Code:
File file = new File();
Document document = new Document();
document.setFile(file);
PersistentTransaction transaction = session.beginTransaction();
session.saveOrUpdate(file); // <- shouldn´t this be the solution of my problem?
session.saveOrUpdate(document);
transaction.commit();
Sure I could solve the problem by adding the following cascade, but I will avoid it if possible:
Code:
@org.hibernate.annotations.Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DELETE, org.hibernate.annotations.CascadeType.LOCK })
@ManyToOne(targetEntity = Fileh.class, fetch = FetchType.LAZY)
@JoinColumns({ @JoinColumn(name = "`FILEHID`", referencedColumnName = "`ID`") })
private File fileh;
Do you have any suggestions?
Thanks in advance and regards,
Vincent