I changed the code to use standard hibernate API code and I still get the error. Here is the code I used:
Code:
Company returnValue = (Company)session.saveOrUpdateCopy(company);
session.flush();
session.connection().commit();
session.close();
I debugged what was happening in SessionImpl, in particular, in doCopy(Object object, Serializable id, Map copiedAlready) during the cascade.
1. The object starts out as a Contact proxy, but line 4045 of SessionImpl (li.getImplementation()) converts it to its true implementation of a Contact object.
2. Since the Contact object is not new (I'm updating it), it goes to line 4067 where the code gets the result by id and class.
3. Line 4073 of SessionImpl, where the code attempts to unproxy the result from step 2 is where the exception occurs. Since the result loaded in step 2 is a proxy for Contact (since Contact is loaded lazily) and since the get in step 2 does not touch the proxy or uninitialize it, the code is unable to unproxy it which yields the exception. The actual exception occurs at line 996 of SessionImpl in the unproxy method.
It doesn't seem to matter whether I put a proxy or the true implementation of the Contact object in the Company object when I save. The only way I can get this to work is by either making Contact not lazy or by forcing an outer join in the many-to-one mapping of Company to Contact.
Thanks for your help,
Rexxe