Fix bug #478
Avoid clearing references on BaseObject in __sleep method
because this breaks setting foreign references. By cloning the
BaseObject itself we ensure the the Baseobject itself is not
changing when calculating the has code
Added Unit-Tests for the bug