Skip to main content

Posts

Showing posts from October, 2012

Disable hibernate Optimistic Lock for specific scenario

Optimistic Lock gives more control on concurrent modification on data. That can be achieved very easily in hibernate using @Version column. But in some cases we may have to avoid concurrency check, example background process. To disable Optimistic lock set OptimisticLockMode as NONE for entity class while hibernate is initiating. We can't remove version column that needs Database changes and ORM changes. In this way we can disable Optimistic Lock by code. implement "org.hibernate.event.Initializable" interface override the below method.     @Override     public void initialize(Configuration cfg) {         Iterator<?> persistentClassIterator = cfg.getClassMappings();         while (persistentClassIterator.hasNext()) {             PersistentClass persistentClass = PersistentClass.class.cast(persistentClassIterator.next());                     Iterator propertyIterator = persistentClass.getDeclaredPropertyIterator();                    while (proper