Can Hibernate @Version account for changes in related objects?

I have 2 objects: Parentand Childin a one-to-many relationship. Parenthas a version, that is, has a @Version field. My goal is to synchronize changes both with objects Parentand Childin the version Parent.

eg. one thread updates Parent, and the other updates one of them Childs, this should throw an OptimisticLockException.

Is it possible?

I tried adding @PreUpdate to Child, which would increase its version Parent, but that did not help, because Hibernate seems to execute listeners only after it checks the versions so that the transaction successfully completes in any case.

If possible, how can this be implemented?

+3
source share
2 answers

You tried to make a child component instead of an object ?

Hibernate default operations can be much more consistent with your requirements. The idea is that the Parent is a real entity, and the Child is considered an element of a larger whole.

In Hibernate, this should be considered the default parent-child relationship. Parental relationships between subjects are less natural, although this is possible.

+1
source

First of all, there are two questions that need to be clarified:

  • , , Child, (, / ). .

  • " " " ", , . , : ( , 1); , . , .

1 , , , , , , . " ".

, , EntityManager.lock() Child. . LockModeType Hibernate EntityManager docs .

+1

Source: https://habr.com/ru/post/1717247/


All Articles