Is it possible to change an object's class from a generalised form to a specialised form without changing the identifier?
I have two classes, a Dog and an Animal. A Dog extends Animal. I am currently using joined table inheritance.
At initial data entry time, all I know is that I am importing animals. An animal object is created and saved to the database. Then, various objects of other classes link to the animal. Finally, I find out that the animal is really a dog, so I want to change the specialise the object.
What I have tried is to implement a constructor to Dog that takes an Animal as a parameter. This copies all the attributes, including the identifier. However when I call saveOrUpdate, I get a StaleObjectException.
I accept that what I am trying to do in Java terms is impossible - as the construction process would create a new object, and any list of objects that had been previously created would still contain a reference to the original identifier. However it is something that can be done in SQL world by creating a new row in the joined table.
Is this possible or am I trying to do something really stupid. My alternatives appear to be to either create a new persistent object, find all references to the original one and move them over, and then delete the original object, all within the same transaction; or remove the inheritance and let a Dog contain an animal as a property.
Thanks
alan
|