The best way to update reference to an entity in hibernate
Imagine I have an hibernate entity that is referenced by many other entities. Now I create new entity and I want new entity to toake plac开发者_如何学Pythone of old one so it is being referenced by others in place of the old one.
What is the best way to do this?
The simplest way would be to use an update statement on the referring entities, for example:
update ReferringEntity re set re.entity.id = :newId where re.entity.id = :oldId
I think you should deep copy(which ever way you preffer to do this) original entity set the state property to deleted and make hibernate to update the entity. Then do whatever modification to copied entity and make hibernate to save this entity. Result will be that original entity will be deleted and copied will be added as new with original references.
精彩评论