Session.saveOrUpdateCopy(object) overwrites the parameter object if the mapping for its class defines an embedded composite-id (key not stored in a separate object, but is the object itself).
I have debugged inside hibernate code and notice that saveOrUpdateCopy() retrieved first the obejct from the database (so far ok). To retrieve the object from the db, hibernate gets frist the object identifier. Since the mapping for this table is defined with an embedded composite, the metdor ClassPersister.getIdentifier(object) returns the same object reference. the problem seems to be in SessionImpl.doCopy() line 04083:
result = get(clazz, id);
This method overwrites the contest of if, which is a reference to object , thereofre the input parameter is overwritten.
The bug does not happens with simple ids, or composite-ids defined in a separate Id class.
A workaround for the bug is to use instead the method session.saveOrUpdateCopy(aObject, id2), by getting the objet identifier and duplicating it (I did it with serialization which is slow) to another object (id2) so that id2 is overwrriten but the original object not.
JDK "1.4.2_08". Windows XP Home. jTDS-1.1.jar