Home  >  Hibernate

Example of @OptimisticLock in Hibernate

By Arvind Rai, May 29, 2013
@OptimisticLock in hibernate stops version. Versioning is done for dirty checking. But in some case like in collection , if we need not dirty check, we can use @OptimisticLock. In the example there is association between two entity. It can be used as
 @OptimisticLock(excluded=true)
 
Let's see how to use in an entity.

Country.java
@Entity
public class Country implements Serializable {
	private static final long serialVersionUID = 1L;
	@Id
	@Column(name="id")
	private int id;
	
	@Column(name="name")
	private String name;
	
	@OneToMany(cascade=CascadeType.ALL)
	@JoinColumn( name="id")
	@OptimisticLock(excluded=true)
	private Set<State> states;
}
 


State.java
@Entity
@Table(name = "state")
public class State  implements Serializable{
	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue
	@Column(name = "id")
    private int id;

	@Column(name="name")
	private String name;
}
 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS







Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us