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.

User.java
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;

@Entity
@Table(name="user")  
public class User {
	@Id
	@GeneratedValue
	private int id;
		
	@Column(name="name")
	private String name;
	
	@Version
        @Column(name="OPTLOCK")
        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;
	}
}
 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us