openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Albert Lee <allee8...@gmail.com>
Subject Re: When is the @Version field of a 'parent' entity updated?
Date Mon, 02 Jun 2014 13:46:37 GMT
@Version is used to support optimistic locking semantics at the object
level and bear no implication to db row or ownership.

"In what circumstances would the Parent version be bumped by one?"

When an entity is changed and needs to be updated back to the database row,
the version field must be incremented to avoid updating row that may have
been updated by another client or catch the case where another client
already had the row updated.

"Is there an option in OpenJPA to turn off bumping of the parent version?"

You can disable optimistic lock versioning by removing @Version from the
version field.  However you are risking overwriting object content in the
db unless your application can guarantee this scenario would not happen.

Albert.


On Mon, Jun 2, 2014 at 3:52 AM, Rupert Smith <rupertlssmith@googlemail.com>
wrote:

> Hi,
>
> I have classes mapped like this:
>
> class Parent {
>    @Id
>    int parentId;
>
>    @Version
>    int version;
>
>    @OneToMany(...)
>    List<Child> children;
> }
>
> class Child
> {
>    @Id
>    int childId;
>
>    ... // no relationship to parent, its uni-directional
> }
>
> The foreign key is on the CHILD table, but in JPA parlance the 'owner' of
> the relationship is the Parent, since it is uni-directional. It is kind of
> implied by this that this is a relationship by composition; the Child is
> part of the Parent and does not exist independently of it.
>
> We noticed that when a new Child is added to a Parent, the version stamp on
> the parent is bumped by one. From a database point of view this seems
> incorrect, since only the CHILD table is modified, and PARENT does not need
> to be touched. From a OO point of view, this seems correct, since the list
> on the Parent was updated, so Parent has changed.
>
> http://en.wikibooks.org/wiki/Java_Persistence/Locking#Cascaded_Locking
>
> Sheds some light on the issue:
>
> "Locking objects is different than locking rows in the database. An object
> can be more complex than a simple row; an object can span multiple tables,
> have inheritance, have relationships, and have dependent objects. So
> determining when an object has *changed* and needs to update its' version
> can be more difficult than determining when a row has changed."
>
> So my question is:
>
> In what circumstances would the Parent version be bumped by one?
>
> Does it happen on all relationships, or only to the side of a relationship
> considered to be the 'owner'. For example, If I put a @ManyToOne link back
> from Child to Parent, so that Child is now the 'owner' of the relationship,
> and holds the foreign key, it is kind of implied that this is a
> relationship by aggregation and the Child is independent. Would the Parent
> version still get bumped by one in this case?
>
> Is there an option in OpenJPA to turn off bumping of the parent version?
>
> Thanks for your help.
>
> Rupert
>



-- 
Albert Lee.

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message