Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Optimistic lock value and cascade

The version field is normally only updated if something in the object's row

The JPA spec is somewhat fuzzy on this.  Originally EclipseLink only updated
the version if the object's row changed.  But then for JPA this was expanded
to include "owned" relationships, which is also somewhat fuzzy, but includes
OneToOne (unless read-only/target foreign key), ManyToOne, ManyToMany,
ElementCollection.  So does not include OneToMany (unless it is
uni-directional or uses a join table).  Basically anything without a
mappedBy is assumed to be owned, and anything with a mappedBy is assumed to
be not owned.

This is all configurable however, in the VersionLockingPolicy class there is
an API setLockOnChangeMode() that can be set to NONE (no relationships),
OWNING (default for JPA), ALL (all).  So you could set this to ALL.  There
is currently no annotation support for this option, so please log a bug for
this, for now you can use a DescriptorCustomizer to set it.

There is also a cascade option on the EclipseLink @OptimisticLocking
annotation that configure an update to the version if any relationship
changes, or any "privateOwned" (deleteOrphans) object is changed.

In JPA you can also use the EntityManager lock() API to selectively have the
version updated for a parent object in your app, which I think is the
technical correct JPA way to do this.


Michael Simons wrote:
> Hello,
> Please, can anybody tell me which configurations determine whether the
> @Version field of a
> related object is set.
> E.g., two entity classes
> class A {
>   @OneToMany(cascade={CascadeType.MERGE, CascadeType.REFRESH,
> CascadeType.REMOVE},
>       fetch=FetchType.LAZY, mappedBy="parent")
>   private List details;
>   ...
>   @Version @Column(name = "jdo_version", nullable = false)
>   private short version;
> }
> class B {
>   @ManyToOne(fetch = FetchType.LAZY, cascade={CascadeType.PERSIST,
> CascadeType.MERGE,
>       CascadeType.MERGE, CascadeType.REFRESH})
>   @JoinColumn(name = "a_id")
>   private A parent = null;
> }
> When a detail (B instance) is created and inserted into the list
> "details", should A.version be
> updated?
> I guessed yes, but that's does not seem to be true.
> The only hint on this issue that I found in the specification follows
> below, but it does _not_
> determine the behaviour of the implementation regarding the version field.
> "If X is a managed entity, it is synchronized to the database.
> For all entities Y referenced by a relationship from X, if the
> relationship to Y has been
> annotated with the cascade element value cascade=PERSIST or cascade=ALL,
> the persist operation
> is applied to Y."
> (see JSR 317: JavaTM Persistence API, Version 2.0, 11.1.25)
> How is it done in EclipseLink?
> Kind Regards, Michael

----- James Sutherland
 EclipseLink ,
Wiki: EclipseLink , TopLink 
Forums: TopLink , EclipseLink 
Book: Java Persistence 
View this message in context:
Sent from the EclipseLink - Users mailing list archive at

Back to the top