Home  >  Hibernate

Versioning for Optimistic Locking in Hibernate

By Arvind Rai, May 12, 2013
Optimistic Locking in Hibernate is if a thread is going to update a resource status, it will not lock the resource. The thread will save the version of the resource of that time and when thread will try to commit the status of resource, thread will again check the versioning of resource, if it is same then status is committed else transaction is roll backed. Now letís see in the example how persistent class keeps implement the version of the resource.

package com.concretepage.persistence;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Version;

public class User {
	private int id;
	private String name;
        public Long getVersion() { 
	   return System.currentTimeMillis();

	public int getId() {
		return id;

	public void setId(int id) {
		this.id = id;

	public String getName() {
		return name;

	public void setName(String name) {
		this.name = name;

©2022 concretepage.com | Privacy Policy | Contact Us