You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Rupert Smith <ru...@googlemail.com> on 2014/06/02 10:52:55 UTC

When is the @Version field of a 'parent' entity updated?

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

Re: When is the @Version field of a 'parent' entity updated?

Posted by Rupert Smith <ru...@googlemail.com>.
On 2 June 2014 14:46, Albert Lee <al...@gmail.com> wrote:

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

You seem to have missed the point.

I know what optimistic locking does and what it is for.

I am seeing situations where a Child is modified, and no change is made to
the Parent, but still the Parent version still gets bumped.

What I want to know, is under what circumstances does OpenJPA consider a
change to the Child, to be a change to the Parent, and is this rippling of
changes up to parent possible to disable, without entirely disabling the
optimistic locking on Parent or Child.

Rupert

Re: When is the @Version field of a 'parent' entity updated?

Posted by Albert Lee <al...@gmail.com>.
@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 <ru...@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.