@ -1016,7 +1016,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
: collection . update ( queryObj , updateObj , upsert , multi , writeConcernToUse ) ;
: collection . update ( queryObj , updateObj , upsert , multi , writeConcernToUse ) ;
if ( entity ! = null & & entity . hasVersionProperty ( ) & & ! multi ) {
if ( entity ! = null & & entity . hasVersionProperty ( ) & & ! multi ) {
if ( writeResult . getN ( ) = = 0 ) {
if ( writeResult . getN ( ) = = 0 & & dbObjectContainsVersionProperty ( queryObj , entity ) ) {
throw new OptimisticLockingFailureException ( "Optimistic lock exception on saving entity: "
throw new OptimisticLockingFailureException ( "Optimistic lock exception on saving entity: "
+ updateObj . toMap ( ) . toString ( ) + " to collection " + collectionName ) ;
+ updateObj . toMap ( ) . toString ( ) + " to collection " + collectionName ) ;
}
}
@ -1031,14 +1031,21 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
private void increaseVersionForUpdateIfNecessary ( MongoPersistentEntity < ? > persistentEntity , Update update ) {
private void increaseVersionForUpdateIfNecessary ( MongoPersistentEntity < ? > persistentEntity , Update update ) {
if ( persistentEntity ! = null & & persistentEntity . hasVersionProperty ( ) ) {
if ( persistentEntity ! = null & & persistentEntity . hasVersionProperty ( ) ) {
if ( ! dbObjectContainsVersionProperty ( update . getUpdateObject ( ) , persistentEntity ) ) {
String versionPropertyField = persistentEntity . getVersionProperty ( ) . getFieldName ( ) ;
update . inc ( persistentEntity . getVersionProperty ( ) . getFieldName ( ) , 1L ) ;
if ( ! update . getUpdateObject ( ) . containsField ( versionPropertyField ) ) {
update . inc ( versionPropertyField , 1L ) ;
}
}
}
}
}
}
private boolean dbObjectContainsVersionProperty ( DBObject dbObject , MongoPersistentEntity < ? > persistentEntity ) {
if ( persistentEntity = = null | | ! persistentEntity . hasVersionProperty ( ) ) {
return false ;
}
return dbObject . containsField ( persistentEntity . getVersionProperty ( ) . getFieldName ( ) ) ;
}
public void remove ( Object object ) {
public void remove ( Object object ) {
if ( object = = null ) {
if ( object = = null ) {