7

I have two classes say Foo and Bar mapped as @OneToOne (bidirectional) using Hibernate (3.6.1 final) with JPA (2.0) like -

@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 } 

Note that - Bar and Qux doesn't have @Version column

If we update Bar then hibernate will not increment the version of Foo and same for Qux. But our business logic needs - if someone updates Bar in the Foo and other thread is trying to update the Qux of the same Foo but doesn't have updated Bar and vice versa then such updates should fail.
Since hibernate doesn't update the version property of Foo if we update the Bar, we decided to update the version of Foo manually (I know it's pretty weird and not recommended) if we update the Bar and Qux.
It works perfectly fine.. but I'm worry about some corner cases in concurrency which may fail this or will have unintended behavior.
Is it safe to do this kind of tweak with version for this purpose? OR is there any other better alternative to this (I've already tried optimistic/pessimistic force increament)

1 Answer 1

11

The correct way to force version update is the following:

em.lock(entity, LockModeType.OPTIMISTIC_FORCE_INCREMENT); 

It's intended to be used in such cases.

Other methods of EntityManager that take LockModeType can be used as well.

Sign up to request clarification or add additional context in comments.

2 Comments

but to use this I need a version property in Bar and Qux, right?
I am trying to use this code: Foo entity = new Foo(); em.persist(foo); int oldVersion = entity.getVersion(); em.lock(entity, LockModeType.OPTIMISTIC_FORCE_INCREMENT); return oldVersion == entity.getVersion(); I expect this to be false, but it is true.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.