Manually updating sleep mode

I have two classes: Foo and Bar are displayed as @OneToOne (bidirectional) using Hibernate (3.6.1 final) with JPA (2.0), for example -

@Entity public class Foo{ @Id private Long id; @OneToOne(cascade = CascadeType.ALL, mappedBy = "foo") private Bar bar; @OneToOne(cascade = CascadeType.ALL, mappedBy = "foo") private Qux qux; @Version private int version; // getters and setters omitted } @Entity public class Bar{ @Id private Long id; @OneToOne @JoinColumn(name = "foo_id", nullable = false) private Foo foo; // getters and setters omitted } @Entity public class Qux { @Id private Long id; @OneToOne @JoinColumn(name = "foo_id", nullable = false) private Foo foo; // getters and setters omitted } 

Please note that in Bar and Qux there is no @Version column

If we update Bar, then hibernate will not increase the version of Foo and the same for Qux. But our business logic requires that if someone updates Bar in Foo and another thread tries to update Qux of the same Foo, but does not have an updated panel, and vice versa, such updates will not work.
Since hibernate does not update the property of the Foo version, if we update Bar, we decided to update the Foo version manually (I know that it is rather strange and not recommended) if we update Bar and Qux.
It works great. But I'm worried about some cases in the concurrency area that may fail or have unintended behavior.
Is it safe to use this setting with the version for this purpose? OR is there any other better alternative to this (I've already tried an optimistic / pessimistic increase in strength)

+7
java hibernate
source share
1 answer

The correct way to force version upgrade is:

 em.lock(entity, LockModeType.OPTIMISTIC_FORCE_INCREMENT); 

It is intended for use in such cases.

Other EntityManager methods that accept LockModeType can also be used.

+10
source share

All Articles