Hi i am working on a project and i have to handle a case when 2 users are on the same screen working on the same orders. If user 1 submits or updates an order first then i don't want user 2 to submit the same order, but instead give him a warning that these orders are already submitted and then refresh the page. Do you guys think if this is correct approach and also how do i go about implementing this in Java and hibernate.?
Asked
Active
Viewed 86 times
1 Answers
0
There is a concept of Version
in Hibernate. Or what is called optimisting locking.
Each entity has a version field on it. ie., a column in each of the tables to store this version. When saving the entity to the DB hibernate will handle the vrsioning. if anything is off, it will throw an exception.
@Entity
public class Flight implements Serializable {
...
@Version
@Column(name="OPTLOCK")
public Integer getVersion() { ... }
}
REF: http://docs.jboss.org/hibernate/orm/4.0/devguide/en-US/html/ch05.html#d0e2225

Zeus
- 6,386
- 6
- 54
- 89
-
Oh thanks. Do you think interceptors or event listeners can be used as well in this case? – jimmyreddevil Mar 03 '15 at 23:50
-
@jimmyreddevil I think so, but, you dont have to do anything special except send the version in an entity to the client as a detached object and when the client saves the detached object send it back to the hibernate sessoin to save it(keeping the version intact in the round trip). While saving, hibernate compares the value in the DB to that of the detached object, if there is any discrepancies, it will throw exception. – Zeus Mar 04 '15 at 00:21