Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Hibernate Collections: Optimistic Locking

DZone's Guide to

Hibernate Collections: Optimistic Locking

· Database Zone
Free Resource

Learn how to create flexible schemas in a relational database using SQL for JSON.

Introduction

Hibernate provides an optimistic locking mechanism to prevent lost updates even for long-conversations. In conjunction with an entity storage, spanning over multiple user requests (extended persistence context or detached entities) Hibernate can guarantee application-level repeatable-reads.

The dirty checking mechanism detects entity state changes and increments the entity version. While basic property changes are always taken into consideration, Hibernate collections are more subtle in this regard.

Owned vs. Inverse Collections

In relational databases, two records are associated through a foreign key reference. In this relationship, the referenced record is the parent while the referencing row (the foreign key side) is the child. A non-null foreign key may only reference an existing parent record.

In the Object-oriented space this association can be represented in both directions. We can have a many-to-one reference from a child to parent and the parent can also have a one-to-many children collection.

Because both sides could potentially control the database foreign key state, we must ensure that only one side is the owner of this association. Only the owningside state changes are propagated to the database. The non-owning side has been traditionally referred as the inverse side.

Next I’ll describe the most common ways of modelling this association.

The Unidirectional Parent-Owning-Side-Child Association Mapping

Only the parent side has a @OneToMany non-inverse children collection. The child entity doesn’t reference the parent entity at all.

@Entity(name = "post")
public class Post {
    ...
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Comment> comments = new ArrayList<Comment>();
    ...
} 

The Unidirectional Parent-Owning-Side-Child Component Association Mapping Mapping

The child side doesn’t always have to be an entity and we might model it as acomponent type instead. An Embeddable object (component type) may contain both basic types and association mappings but it can never contain an @Id. The Embeddable object is persisted/removed along with its owning entity.

The parent has an @ElementCollection children association. The child entity may only reference the parent through the non-queryable Hibernate specific @Parentannotation.

@Entity(name = "post")
public class Post {
    ...
    @ElementCollection
    @JoinTable(name = "post_comments", joinColumns = @JoinColumn(name = "post_id"))
    @OrderColumn(name = "comment_index")
    private List<Comment> comments = new ArrayList<Comment>();
    ...

    public void addComment(Comment comment) {
        comment.setPost(this);
        comments.add(comment);
    }
}

@Embeddable
public class Comment {
    ...
    @Parent
    private Post post;
    ...
}

The Bidirectional Parent-Owning-Side-Child Association Mapping

The parent is the owning side so it has a @OneToMany non-inverse (without a mappedBy directive) children collection. The child entity references the parent entity through a @ManyToOne association that’s neither insertable nor updatable:

@Entity(name = "post")
public class Post {
    ...
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Comment> comments = new ArrayList<Comment>();
    ... 

    public void addComment(Comment comment) {
        comment.setPost(this);
        comments.add(comment);
    }
} 

@Entity(name = "comment")
public class Comment 
    ...
    @ManyToOne
    @JoinColumn(name = "post_id", insertable = false, updatable = false)
    private Post post;
    ...
}

The Bidirectional Parent-Owning-Side-Child Association Mapping

The child entity references the parent entity through a @ManyToOne association, and the parent has a mappedBy @OneToMany children collection. The parent side is the inverse side so only the @ManyToOne state changes are propagated to the database.

Even if there’s only one owning side, it’s always a good practice to keep both sides in sync by using the add/removeChild() methods.

@Entity(name = "post")
public class Post {
    ...
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "post")
    private List<Comment> comments = new ArrayList<Comment>();
    ...
    public void addComment(Comment comment) {
        comment.setPost(this);
        comments.add(comment);
    }
}

@Entity(name = "comment")
public class Comment {
    ...
    @ManyToOne
    private Post post; 
    ...
}

The Unidirectional Parent-Owning-Side-Child Association Mapping

The child entity references the parent through a @ManyToOne association. The parent doesn’t have a @OneToMany children collection so the child entity becomes the owning side. This association mapping resembles the relational data foreign key linkage.

@Entity(name = "comment")
public class Comment {
    ...
    @ManyToOne
    private Post post; 
    ...
}

Collection Versioning

The 3.4.2 section of the JPA 2.1 specification defines optimistic locking as:

The version attribute is updated by the persistence provider runtime when the object is written to the database. All non-relationship fields and proper ties and all relationships owned by the entity are included in version checks[35].

[35] This includes owned relationships maintained in join tables

N.B. Only owning-side children collection can update the parent version.

Testing Time

Let’s test how the parent-child association type affects the parent versioning. Because we are interested in the children collection dirty checking, theunidirectional child-owning-side-parent association is going to be skipped, as in that case the parent doesn’t contain a children collection.

Test Case

The following test case is going to be used for all collection type use cases:

protected void simulateConcurrentTransactions(final boolean shouldIncrementParentVersion) {
    final ExecutorService executorService = Executors.newSingleThreadExecutor();

    doInTransaction(new TransactionCallable<Void>() {
        @Override
        public Void execute(Session session) {
            try {
                P post = postClass.newInstance();
                post.setId(1L);
                post.setName("Hibernate training");
                session.persist(post);
                return null;
            } catch (Exception e) {
                throw new IllegalArgumentException(e);
            }
        }
    });

    doInTransaction(new TransactionCallable<Void>() {
        @Override
        public Void execute(final Session session) {
            final P post = (P) session.get(postClass, 1L);
            try {
                executorService.submit(new Callable<Void>() {
                    @Override
                    public Void call() throws Exception {
                        return doInTransaction(new TransactionCallable<Void>() {
                            @Override
                            public Void execute(Session _session) {
                                try {
                                    P otherThreadPost = (P) _session.get(postClass, 1L);
                                    int loadTimeVersion = otherThreadPost.getVersion();
                                    assertNotSame(post, otherThreadPost);
                                    assertEquals(0L, otherThreadPost.getVersion());
                                    C comment = commentClass.newInstance();
                                    comment.setReview("Good post!");
                                    otherThreadPost.addComment(comment);
                                    _session.flush();
                                    if (shouldIncrementParentVersion) {
                                        assertEquals(otherThreadPost.getVersion(), loadTimeVersion + 1);
                                    } else {
                                        assertEquals(otherThreadPost.getVersion(), loadTimeVersion);
                                    }
                                    return null;
                                } catch (Exception e) {
                                    throw new IllegalArgumentException(e);
                                }
                            }
                        });
                    }
                }).get();
            } catch (Exception e) {
                throw new IllegalArgumentException(e);
            }
            post.setName("Hibernate Master Class");
            session.flush();
            return null;
        }
    });
}

The Unidirectional Parent-Owning-Side-Child Association Testing

#create tables
Query:{[create table comment (idbigint generated by default as identity (start with 1), review varchar(255), primary key (id))][]}
Query:{[create table post (idbigint not null, name varchar(255), version integer not null, primary key (id))][]}
Query:{[create table post_comment (post_id bigint not null, comments_id bigint not null, comment_index integer not null, primary key (post_id, comment_index))][]}
Query:{[alter table post_comment add constraint FK_se9l149iyyao6va95afioxsrl foreign key (comments_id) references comment][]}
Query:{[alter table post_comment add constraint FK_6o1igdm04v78cwqre59or1yj1 foreign key (post_id) references post][]}

#insert post in primary transaction
Query:{[insert into post (name, version, id) values (?, ?, ?)][Hibernate training,0,1]}

#select post in secondary transaction
Query:{[selectentityopti0_.idas id1_1_0_, entityopti0_.name as name2_1_0_, entityopti0_.version as version3_1_0_ from post entityopti0_ where entityopti0_.id=?][1]}

#insert comment in secondary transaction
#optimistic locking post version update in secondary transaction
Query:{[insert into comment (id, review) values (default, ?)][Good post!]}
Query:{[update post setname=?, version=? where id=? and version=?][Hibernate training,1,1,0]}
Query:{[insert into post_comment (post_id, comment_index, comments_id) values (?, ?, ?)][1,0,1]}

#optimistic locking exception in primary transaction
Query:{[update post setname=?, version=? where id=? and version=?][Hibernate Master Class,1,1,0]}
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.vladmihalcea.hibernate.masterclass.laboratory.concurrency.EntityOptimisticLockingOnUnidirectionalCollectionTest$Post#1]

The Unidirectional Parent-Owning-Side-Child Component Association Testing

#create tables
Query:{[create table post (idbigint not null, name varchar(255), version integer not null, primary key (id))][]}
Query:{[create table post_comments (post_id bigint not null, review varchar(255), comment_index integer not null, primary key (post_id, comment_index))][]}
Query:{[alter table post_comments add constraint FK_gh9apqeduab8cs0ohcq1dgukp foreign key (post_id) references post][]}

#insert post in primary transaction
Query:{[insert into post (name, version, id) values (?, ?, ?)][Hibernate training,0,1]}

#select post in secondary transaction
Query:{[selectentityopti0_.idas id1_0_0_, entityopti0_.name as name2_0_0_, entityopti0_.version as version3_0_0_ from post entityopti0_ where entityopti0_.id=?][1]}
Query:{[selectcomments0_.post_id as post_id1_0_0_, comments0_.review as review2_1_0_, comments0_.comment_index as comment_3_0_ from post_comments comments0_ where comments0_.post_id=?][1]}

#insert comment in secondary transaction
#optimistic locking post version update in secondary transaction
Query:{[update post setname=?, version=? where id=? and version=?][Hibernate training,1,1,0]}
Query:{[insert into post_comments (post_id, comment_index, review) values (?, ?, ?)][1,0,Good post!]}

#optimistic locking exception in primary transaction
Query:{[update post setname=?, version=? where id=? and version=?][Hibernate Master Class,1,1,0]}
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.vladmihalcea.hibernate.masterclass.laboratory.concurrency.EntityOptimisticLockingOnComponentCollectionTest$Post#1] 

The Bidirectional Parent-Owning-Side-Child Association Testing

#create tables
Query:{[create table comment (idbigint generated by default as identity (start with 1), review varchar(255), post_id bigint, primary key (id))][]}
Query:{[create table post (idbigint not null, name varchar(255), version integer not null, primary key (id))][]}
Query:{[create table post_comment (post_id bigint not null, comments_id bigint not null)][]}
Query:{[alter table post_comment add constraint UK_se9l149iyyao6va95afioxsrl  unique (comments_id)][]}
Query:{[alter table comment add constraint FK_f1sl0xkd2lucs7bve3ktt3tu5 foreign key (post_id) references post][]}
Query:{[alter table post_comment add constraint FK_se9l149iyyao6va95afioxsrl foreign key (comments_id) references comment][]}
Query:{[alter table post_comment add constraint FK_6o1igdm04v78cwqre59or1yj1 foreign key (post_id) references post][]}

#insert post in primary transaction
Query:{[insert into post (name, version, id) values (?, ?, ?)][Hibernate training,0,1]}

#select post in secondary transaction
Query:{[selectentityopti0_.idas id1_1_0_, entityopti0_.name as name2_1_0_, entityopti0_.version as version3_1_0_ from post entityopti0_ where entityopti0_.id=?][1]}
Query:{[selectcomments0_.post_id as post_id1_1_0_, comments0_.comments_id as comments2_2_0_, entityopti1_.idas id1_0_1_, entityopti1_.post_id as post_id3_0_1_, entityopti1_.review as review2_0_1_, entityopti2_.idas id1_1_2_, entityopti2_.name as name2_1_2_, entityopti2_.version as version3_1_2_ from post_comment comments0_ inner joincomment entityopti1_ on comments0_.comments_id=entityopti1_.idleft outer joinpost entityopti2_ on entityopti1_.post_id=entityopti2_.idwhere comments0_.post_id=?][1]}

#insert comment in secondary transaction
#optimistic locking post version update in secondary transaction
Query:{[insert into comment (id, review) values (default, ?)][Good post!]}
Query:{[update post setname=?, version=? where id=? and version=?][Hibernate training,1,1,0]}
Query:{[insert into post_comment (post_id, comments_id) values (?, ?)][1,1]}

#optimistic locking exception in primary transaction
Query:{[update post setname=?, version=? where id=? and version=?][Hibernate Master Class,1,1,0]}
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.vladmihalcea.hibernate.masterclass.laboratory.concurrency.EntityOptimisticLockingOnBidirectionalParentOwningCollectionTest$Post#1]

The Bidirectional Parent-Owning-Side-Child Association Testing

#create tables
Query:{[create table comment (idbigint generated by default as identity (start with 1), review varchar(255), post_id bigint, primary key (id))][]}
Query:{[create table post (idbigint not null, name varchar(255), version integer not null, primary key (id))][]}
Query:{[alter table comment add constraint FK_f1sl0xkd2lucs7bve3ktt3tu5 foreign key (post_id) references post][]}

#insert post in primary transaction
Query:{[insert into post (name, version, id) values (?, ?, ?)][Hibernate training,0,1]}

#select post in secondary transaction
Query:{[selectentityopti0_.idas id1_1_0_, entityopti0_.name as name2_1_0_, entityopti0_.version as version3_1_0_ from post entityopti0_ where entityopti0_.id=?][1]}

#insert comment in secondary transaction
#post version is not incremented in secondary transaction
Query:{[insert into comment (id, post_id, review) values (default, ?, ?)][1,Good post!]}
Query:{[selectcount(id) from comment where post_id =?][1]}

#update works in primary transaction
Query:{[update post setname=?, version=? where id=? and version=?][Hibernate Master Class,1,1,0]}

If you enjoy reading this article, you might want to subscribe to my newsletter and get a discount for my book as well.

Vlad Mihalcea&apos;s Newsletter

Overruling Default Collection Versioning

If the default owning-side collection versioning is not suitable for your use case, you can always overrule it with Hibernate [a href="http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#d0e2903" style="font-family: inherit; font-size: 14px; font-style: inherit; font-weight: inherit; text-decoration: none; color: rgb(1, 160, 219); -webkit-tap-highlight-color: rgb(240, 29, 79); background: transparent;"]@OptimisticLock annotation.

Let’s overrule the default parent version update mechanism for bidirectional parent-owning-side-child association:

@Entity(name = "post")
public class Post {
    ...
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @OptimisticLock(excluded = true)
    private List<Comment> comments = new ArrayList<Comment>();
    ...

    public void addComment(Comment comment) {
        comment.setPost(this);
        comments.add(comment);
    }
}   

@Entity(name = "comment")
public class Comment {
    ...
    @ManyToOne
    @JoinColumn(name = "post_id", insertable = false, updatable = false)
    private Post post;
    ...
}

This time, the children collection changes won’t trigger a parent version update:

#create tables
Query:{[create table comment (idbigint generated by default as identity (start with 1), review varchar(255), post_id bigint, primary key (id))][]}
Query:{[create table post (idbigint not null, name varchar(255), version integer not null, primary key (id))][]}
Query:{[create table post_comment (post_id bigint not null, comments_id bigint not null)][]}
Query:{[]}
Query:{[alter table comment add constraint FK_f1sl0xkd2lucs7bve3ktt3tu5 foreign key (post_id) references post][]}
Query:{[alter table post_comment add constraint FK_se9l149iyyao6va95afioxsrl foreign key (comments_id) references comment][]}
Query:{[alter table post_comment add constraint FK_6o1igdm04v78cwqre59or1yj1 foreign key (post_id) references post][]}

#insert post in primary transaction
Query:{[insert into post (name, version, id) values (?, ?, ?)][Hibernate training,0,1]}

#select post in secondary transaction
Query:{[selectentityopti0_.idas id1_1_0_, entityopti0_.name as name2_1_0_, entityopti0_.version as version3_1_0_ from post entityopti0_ where entityopti0_.id=?][1]}
Query:{[selectcomments0_.post_id as post_id1_1_0_, comments0_.comments_id as comments2_2_0_, entityopti1_.idas id1_0_1_, entityopti1_.post_id as post_id3_0_1_, entityopti1_.review as review2_0_1_, entityopti2_.idas id1_1_2_, entityopti2_.name as name2_1_2_, entityopti2_.version as version3_1_2_ from post_comment comments0_ inner joincomment entityopti1_ on comments0_.comments_id=entityopti1_.idleft outer joinpost entityopti2_ on entityopti1_.post_id=entityopti2_.idwhere comments0_.post_id=?][1]}

#insert comment in secondary transaction
Query:{[insert into comment (id, review) values (default, ?)][Good post!]}
Query:{[insert into post_comment (post_id, comments_id) values (?, ?)][1,1]}

#update works in primary transaction
Query:{[update post setname=?, version=? where id=? and version=?][Hibernate Master Class,1,1,0]}

If you enjoyed this article, I bet you are going to love my book as well.






Conclusion

It’s very important to understand how various modeling structures impact concurrency patterns. The owning side collections changes are taken into consideration when incrementing the parent version number, and you can always bypass it using the @OptimisticLock  annotation.

Code available on GitHub.

If you have enjoyed reading my article and you’re looking forward to getting instant email notifications of my latest posts, you just need to follow my blog.

Create flexible schemas using dynamic columns for semi-structured data. Learn how.

Topics:
sql ,hibernate ,persistence ,database

Published at DZone with permission of Vlad Mihalcea. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}