Hibernate versioning parent entity

前端 未结 4 1310
暖寄归人
暖寄归人 2021-01-13 17:51

Consider two entities Parent and Child.

  • Child is part of Parent\'s transient collection
  • Child has a ManyToOne mapping to parent with FetchType.LAZY
4条回答
  •  Happy的楠姐
    2021-01-13 18:44

    I think I figured it out. After merge is called an attached instance reference is returned. When I obtain an explicit lock for that using entityManager.lock(updated, LockModeType.WRITE); then version number is increased even if Parent instance was not updated in db.

    In addition I am comparing detached instance version with persisted instance version. If they don't match then Parent was updated in db and also version number has changed. This keeps version numbers consistent. Otherwise entityManager.lock would increase version number even if merge operation changed it.

    Still looking for solution how to make hibernate increase version when entity is not dirty during merge.

提交回复
热议问题