How to use the Hibernate optimistic locking version property on the front end?

前端 未结 1 1933
陌清茗
陌清茗 2021-02-07 21:37

Optimistic locking using the version attribute for an entity works fine and is easy to implement:



        
1条回答
  •  忘了有多久
    2021-02-07 22:37

    Do you really need to use DTO? You wouldn't have had this problem if you were passing the actual entity around - nor would you have to load the entity again, which isn't exactly great for performance.

    But even if you do have a legitimate reason to use DTO, I'm not quite grasping why you would try to update the version number on your freshly reloaded entity prior to saving. Consider different scenarios possible in your workflow:

    1. Entity is loaded initially, has version = V1
    2. It's transferred to DTO which goes to UI, comes back and is ready to be saved.
    3. Entity is loaded again, has version = V2

    You have two possibilities now:

    1. V1 == V2. Peachy, you don't have to do anything.
    2. V1 is less than V2, meaning entity was updated by someone else while you were editing it. There's no reason to try to set version to V1 and attempt to save because saving will fail. You can either save it with V2 (thus overriding someone else's changes) or fail now.(without involving Hibernate).

    0 讨论(0)
提交回复
热议问题