I'm using JPA 2 with Hibernate 3.x. I have two Entity objects Foo and Bar and the relation is like this -
@Entity
public class Foo{
@Id
private long id;
@OneToOne(cascade = CascadeType.ALL )
@JoinColumn(name = "bar")
private Bar bar;
@Version
private int version;
}
@Entity
public class Bar{
@Id
private long id;
private String name;
@Version
private int version;
}
Now my problem is If I loaded Foo
from entityManager.find(...)
and updated name
of Bar
like foo.getBar().setName("someAnotherName");
and fire entityManager.merge(foo)
then Hibernate fires a update on Bar
and increments a version for Bar
only. From the object perspective doesn't its a change of the sate of Foo as well and shouldn't it increment the version of Foo as well?
Now I would like to remove the version
property from Bar
and version
of Foo
should get incremented if Bar
gets updated.
How can I achieve this?
In your case not Foo, but entity on which it references has been changed, so there is no need in version increment.
@Version adds optimistic locking capability to an entity and it shouldn't be used as a change counter. The main purpose of this annotation is detecting of conflicting updates, so think twice before doing that.
The only way to override this behaviour is to use [OPTIMISTIC | PESSIMISTIC]_FORCE_INCREMENT