Hi all,
I have relation between two classes like this:
|Class1| <>-[1]--------[*]-> |Class2|
and an annotation on the Class2 end @OneToMany(cascade=ALL),
but I have also one more association from Class1 to Class 2, like this:
|Class1| ------------------[1]-- |Class2|
So, in the Class1 instance, I have collection of Class2 instances (the first diagram), and also I have one another object of the Class2, which is the same as one of the elements in that collection (the second diagram).
But, when I fill Class1 object like this:
Code:
Class2 c = new Class2();
Class2 b = new Class2();
Class2 e = new Class2();
Set<Class2> list = new LinkedHashSet<Class2>();
list.add(c); list.add(b); list.add(e);
Class1 in = new Class1();
in.setList(list); <- this is related to the first diagram
in.setRef(c); <- this is related to the second diagram
hib.persist(in);
I got an error:
Code:
javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.test.Class1.ref
It seems to me that it is trying firstly to get reference to c in databse, but because list of the objects is not yet writen (which containts c), it report this error. Do you maybe know how can I resolve this situation ?
I also tried to add @OneToOne annotations on the ref part (the second association) and it doesn't work.
Thanks in advance!