Hi,
I have two classes with one to many relationship, let's say Parent and Child.
I used the following method to load the Parent from DB
Parent parent = getHibernateTemplate.get(Parent.class, parentId);
Then I created a new Child and set its parent property
child.setParent(parent);
Finally, I tried to save the child with the following code:
getHibernateTemplate.merge(child);
At this point I got the following exception:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: Parent
The thing that I couldn't understand is I just loaded the parent obj from DB, how could it become transient?
I can make that exception disappear by setting the @ManyToOne(cascade=CascadeType.ALL), but that ends up with duplicate Parent objects being saved into the DB.
Thanks for any help.
|