开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜