Hibernate versioning parent entity

前端 未结 4 1305
暖寄归人
暖寄归人 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条回答
  •  时光取名叫无心
    2021-01-13 18:30

    I just implemented something similar which is working like beast fast and nice. At the moment you are saving your 'child' just call do something like:

    save(child);
    T parent = child.getParentEntity();
    entityManager.lock(parent, LockModeType.OPTIMISTIC_FORCE_INCREMENT);
    

    You need access to the entity manager which can be obtained in spring like:

      @PersistenceContext
      private EntityManager entityManager;
    

    Your parent entity should have @Version from javax.persistence.Version and NOT the spring one. (I assume that on the child save moment you will have all the verifications and things done so when you are saving the child the parent should get dirty for sure)

提交回复
热议问题